views:

1361

answers:

2

How do I programmatically select a tableview row so that

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

gets executed? selectRowAtIndexPath will only highlight the row.

+1  A: 

UITableView's selectRowAtIndexPath:animated:scrollPosition: should do the trick.

Just pass UITableViewScrollPositionNone for scrollPosition and the user won't see any movement.


You should also be able to manually run the action:

[theTableView.delegate tableView:theTableView didSelectRowAtIndexPath:indexPath]

after you selectRowAtIndexPath:animated:scrollPosition: so the highlight happens as well as any associated logic.

anq
Sorry, that is what I'm using. I accidentally put scrollToRowAtIndexPath. I've updated the question. scrollToRowAtIndexPath only highlights the cell.
4thSpace
Yeah, I'm sorry. It says it won't fire didSelectRowAtIndexPath right there in the docs link I posted. I need to learn to read.
anq
+6  A: 

From reference documentation:

Calling this method does not cause the delegate to receive a tableView:willSelectRowAtIndexPath: or tableView:didSelectRowAtIndexPath: message, nor will it send UITableViewSelectionDidChangeNotification notifications to observers.

What I would do is:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self doSomethingWithRowAtIndexPath:indexPath];
}

And then, from where you wanted to call selectRowAtIndexPath, you instead call doSomethingWithRowAtIndexPath. On top of that, you can additionally also call selectRowAtIndexPath if you want the UI feedback to happen.

Jaanus
Or when you select the row programmatically, you could call tableView:didSelectRowAtIndexPath: yourself (in the class you have wired as the delegate).
Kendall Helmstetter Gelner