如何使UITableViewCell突出显示状态持久化 [英] How to make UITableViewCell highlighted state persist

查看:69
本文介绍了如何使UITableViewCell突出显示状态持久化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITableviewCell。当用户单击单元格时,我保存索引路径,然后调用cellforrowAtIndexpath方法获取单元格,然后在该单元格上调用SetHighlighted:TRUE。

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?

谢谢

推荐答案

保存所选单元格的索引路径

save the indexpath of the selected cell

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.selectedIndexPath = indexPath;
}

并在中比较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;

}

但是,请记住苹果不鼓励使用单元格突出显示状态以指示所选单元您应该使用 cell.accessoryType = UITableViewCellAccessoryCheckmark;

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;

这篇关于如何使UITableViewCell突出显示状态持久化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆