2012-01-02 119 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

相關問題