UITableView Detect Selected Cell?

扶醉桌前 提交于 2019-12-23 07:28:27

问题


I have multiple UITableViews in my app, is there a method of detecting which cell/row the user has selected in that column?

Also is it possible to programatically deselect a cell/row?

Thanks.


回答1:


Get currently selected index path for a table:

NSIndexPath *path = [tableView indexPathForSelectedRow];

Deselect currently selected row:

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



回答2:


These are all easily found in the documentation for 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




回答3:


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.

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


来源:https://stackoverflow.com/questions/4318386/uitableview-detect-selected-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!