Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make UITableViewCell highlighted state persist

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

like image 908
banditKing Avatar asked Apr 17 '11 16:04

banditKing


1 Answers

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;

like image 55
Matthias Bauch Avatar answered Nov 15 '22 19:11

Matthias Bauch