开发者

How to make UITableViewCell highlighted state persist

开发者 https://www.devze.com 2023-02-27 02:54 出处:网络
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 t开发者_StackOverflow中文版hen call the SetHighligh

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 t开发者_StackOverflow中文版hen 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;

0

精彩评论

暂无评论...
验证码 换一张
取 消