Reusable custom cell

I have a tableView with a custom cell . I have the opportunity to save some elements to my favorites, and when this happens, I want to add an asterisk image to the cell . I tried to do this, but after the appearance of the star, I have a problem. I think this is because of the reusable cell , but I don't know how to solve it. My problem: 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:@" 3081@3x.png "]; } } return cell; } 
+5
source share
3 answers

Instead of cellForRowAtIndexPath replace the following code. you will get your wish.

 - (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:@" 3081@3x.png "]; } } return cell; } 

Hope this helps you.

+2
source

You need to delete the image if the object is not TRUE

 if (isTheObjectThere==TRUE) { cell.favImg.image=[UIImage imageNamed:@" 3081@3x.png "]; } else { cell.favImg.image=nil; } 
+1
source

Yes, you are right because of the reuse of your cells through dequeueReusableCell with an identifier like -

  dictionaryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 

according to your requirements, you will set the star image in the cell to indicate some selected elements in the corresponding cell, for example

 BOOL isTheObjectThere = [self.favoriteArry containsObject:self.tableData[indexPath.row]]; if (isTheObjectThere==TRUE) { cell.favImg.image=[UIImage imageNamed:@" 3081@3x.png "]; } 

If any cell with a star-shaped image is reused, it should be deleted, if the next cell does not have some favorite elements, but if it has some favorite elements, then it should be used as

To solve this problem, just add the else case with the above if statement as

 if (isTheObjectThere == TRUE) cell.favImg.image=[UIImage imageNamed:@" 3081@3x.png "]; else cell.favImg.image=nil; 
+1
source

All Articles