2012-01-02 127 views
8

我想在tableView中选择一个单元格。它正在使用此代码正常工作,但一旦以编程方式选择,它不会触发didSelectRowAtIndexPath事件。我如何触发它?以编程方式选择行时未触发didSelectRowAtIndexPath事件

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
[self.mainTable selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone]; 

回答

22

据工作作为记载:

调用此方法不会导致委托接收tableView:willSelectRowAtIndexPath:tableView:didSelectRowAtIndexPath:消息,也不会发送UITableViewSelectionDidChangeNotification通知观察员。

调用它自己选择的细胞后:

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]; 
+0

工作,感谢您的帮助 – Jaume 2012-01-02 22:36:05

+0

对于未来的旅客:这就是为什么这不起作用很好的解释,但我相信这是一个比较全面的解决方案:http://stackoverflow.com/questions/2305781/iphone-didselectrowatindexpath-not-invoked – kbpontius 2015-06-17 17:40:18

相关问题