Josh Kahane
Josh Kahane

Reputation: 17169

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.

Upvotes: 13

Views: 19655

Answers (3)

zeeawan
zeeawan

Reputation: 6905

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

Upvotes: 2

David Liu
David Liu

Reputation: 9600

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

Upvotes: 3

Jeremy Fuller
Jeremy Fuller

Reputation: 3401

Get currently selected index path for a table:

NSIndexPath *path = [tableView indexPathForSelectedRow];

Deselect currently selected row:

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

Upvotes: 29

Related Questions