如何在同时选择另一个tableview单元格时取消选择tableview单元格?

时间:2017-07-20 05:22:59

标签: ios objective-c uitableview

我是第一次使用

进行表格查看单元格自动选择
  [cell setSelected:YES animated:YES];

首次以编程方式选择单元格。它工作正常。现在,当我在此期间选择另一个单元格时,我想要取消选择该单元格。我怎样才能做到这一点。我知道当我选择一个单元格然后tableview委托方法

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

被调用,当取消选择一个单元格时,“在索引路径上取消选择行”被调用。

但问题是我以编程方式选择单元格,当我选择另一个单元格然后委托方法

 -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

不打电话。

我该怎么做才能解决这个问题,或解决这个问题的逻辑是什么?

2 个答案:

答案 0 :(得分:2)

不要直接在单元格上调用setSelected:animated:,而是调用表格视图的selectRowAtIndexPath:animated:scrollPosition:方法来设置初始选择。

答案 1 :(得分:1)

您可以使用以下方法取消选择行:

[tableView deselectRowAtIndexPath:indexPath animated:YES];

您只需选择存储索引即可。因此,您只需要检查是否已选择indexpath,然后取消选择。

获取NSIndexPath的对象:

NSIndexPath *currentSelectedPath;

更新您的didSelectRow方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(currentSelectedPath)
    {
        [tableView deselectRowAtIndexPath:currentSelectedPath animated:YES];
    }
    currentSelectedPath = indexPath;

 // Do your other stuff
}
相关问题