12

I would like to select a cell in a tableView. It is working properly using this code, but once selected programmatically it does not trigger a didSelectRowAtIndexPath event. How can I trigger it?

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.mainTable selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
Cœur
  • 37,241
  • 25
  • 195
  • 267
Jaume
  • 913
  • 2
  • 17
  • 31

1 Answers1

29

It is working as documented:

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.

Call it yourself after selecting your cell:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

[self tableView:self.mainTable willSelectRowAtIndexPath:indexPath];
[self.mainTable selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
[self tableView:self.mainTable didSelectRowAtIndexPath:indexPath];
Community
  • 1
  • 1
Guillaume
  • 21,685
  • 6
  • 63
  • 95
  • For future travelers: This is a great explanation of why this doesn't work, but I believe this is a more comprehensive solution: http://stackoverflow.com/questions/2305781/iphone-didselectrowatindexpath-not-invoked – kbpontius Jun 17 '15 at 17:40