Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reusable custom cell

I have a tableView with a custom cell. I have the posibility to save to favorites some of the elements , and when this is happening i want to add a star image in cell. I was trying doing this , but after the star appears, i have a problem . I think it's because of reusable cell but i dont know how to solve it . My problem is : stars appear again on the other cells even if the word is not added on favorites.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    dictionaryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell=[[dictionaryTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        cell.textLabel.text = [self.searchResult objectAtIndex:indexPath.row];

    }
    else
    {
        cell.word.text = self.tableData[indexPath.row];
        BOOL isTheObjectThere = [self.favoriteArry containsObject:self.tableData[indexPath.row]];
        if (isTheObjectThere==TRUE) {
             cell.favImg.image=[UIImage imageNamed:@"[email protected]"];
        }
    }

        return cell;

}
like image 397
Adina Marin Avatar asked Sep 28 '22 05:09

Adina Marin


1 Answers

Replace following code in place of cellForRowAtIndexPath. you will get your desire output.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    dictionaryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell=[[dictionaryTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
   cell.favImg.hidden = YES;
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        cell.textLabel.text = [self.searchResult objectAtIndex:indexPath.row];

    }
    else
    {
        cell.word.text = self.tableData[indexPath.row];
        BOOL isTheObjectThere = [self.favoriteArry containsObject:self.tableData[indexPath.row]];
        if (isTheObjectThere==TRUE) {
             cell.favImg.hidden = NO;
             cell.favImg.image=[UIImage imageNamed:@"[email protected]"];
        }
    }

    return cell;

}

Hope this help you.

like image 142
Jatin Patel - JP Avatar answered Nov 10 '22 05:11

Jatin Patel - JP