Question

How to programmatically "tap" a UITableView cell?

I was wondering is there a way that I could have my code "tap" a cell in my UITableView in order to reproduce the behaviour specified in the - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath delegate method.

I guess in other words, is is possible to invoke the - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath delegate method programatically?

 21  25547  21
1 Jan 1970

Solution

 39

if you want to have the cell selected, i.e highlight a specific cell:

//select first row of first section
NSIndexPath* selectedCellIndexPath= [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView selectRowAtIndexPath:selectedCellIndexPath animated:false scrollPosition:UITableViewScrollPositionMiddle];

if you want to additionally trigger actions in your didSelectRowAtIndexPath method, you need to manually call the delegate method, it won't happen automatically:

[self tableView:self.tableView didSelectRowAtIndexPath:selectedCellIndexPath];
2012-01-05

Solution

 21

Can't you put any logic in didSelectRowAtIndexPath into a separate method and just call that method from both didSelectRowAtIndexPath and wherever else you want to call the same code?

2011-05-07