简体   繁体   English

UITableView检测选定的单元格?

[英]UITableView Detect Selected Cell?

I have multiple UITableViews in my app, is there a method of detecting which cell/row the user has selected in that column? 我的应用程序中有多个UITableViews,有没有一种方法可以检测用户在该列中选择了哪个单元格/行?

Also is it possible to programatically deselect a cell/row? 还有可能以编程方式取消选择单元/行吗?

Thanks. 谢谢。

Get currently selected index path for a table: 获取表的当前选定索引路径:

NSIndexPath *path = [tableView indexPathForSelectedRow];

Deselect currently selected row: 取消选择当前选中的行:

[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];

These are all easily found in the documentation for UITableView. 这些都可以在UITableView的文档中轻松找到。 Perhaps you should look there first next time. 也许您下次应该第一次去那里。

Here's even a link: http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html 甚至还有一个链接: http : //developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html

NSIndexPath *path = [tableView indexPathForSelectedRow];

The above line will throw EXC BAD ACCESS if no sell is selected so keep track of your selected cell with NSIndexPath instance variable and only deselect when it is actually selected with isSelected property of cell. 如果未选择任何卖出,则上面的行将引发EXC BAD ACCESS ,因此使用NSIndexPath实例变量跟踪所选单元格,并且仅在使用cell的isSelected属性实际选择时才取消选择。

UITableViewCell *cell = [tableView cellForRowAtIndexPath:someIndexPath];
if(cell.isSelected) {
    [tableView deselectRowAtIndexPath:someIndexPath animated:YES];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM