UITableView如何检测选中的单元格?

13

我在我的应用程序中有多个UITableViews,在该列中是否有检测用户选择的单元格/行的方法?

同时,是否可以以编程方式取消选择单元格/行?

谢谢。

3个回答

29

获取表格当前选定的索引路径:

NSIndexPath *path = [tableView indexPathForSelectedRow];
取消当前选定的行:
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];

3

2
NSIndexPath *path = [tableView indexPathForSelectedRow];

如果未选择任何销售,则上述行将抛出“EXC BAD ACCESS”异常,因此请使用NSIndexPath实例变量跟踪所选单元格,并仅在实际选择了单元格的情况下使用cell的isSelected属性进行取消选择。
UITableViewCell *cell = [tableView cellForRowAtIndexPath:someIndexPath];
if(cell.isSelected) {
    [tableView deselectRowAtIndexPath:someIndexPath animated:YES];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接