简体   繁体   中英

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?

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. 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

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];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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