I have a UITableviewCell. When a user clicks the cell, Im saving the indexpath and then calling the cellforrowAtIndexpath method to get the cell and then call the SetHighlighted:TRUE on that cell.
This works fine but the problem is when I scroll up and down the tableview, the selected cell when reappears, is not highlighted. How do I make the highlighted blue color persist so the user can visually see their selection even after scrolling the table up or down?
Thanks
save the indexpath of the selected cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.selectedIndexPath = indexPath;
}
and compare in tableVIew:cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// configure cell
if ([indexPath isEqual:self.selectedIndexPath]) {
[cell setHighlighted:YES];
}
else {
[cell setHighlighted:NO];
}
return cell;
}
However, keep in mind that apple discourages the use of the cell highlight state to indicate selected cell. You should probably use cell.accessoryType = UITableViewCellAccessoryCheckmark;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With