我正在编写一个带有tab视图中的表视图的iOS应用程序。在我的UITableViewController中,我实现了-tableView:didSelectRowAtIndexPath:,但当我在运行时选择一行时,该方法没有被调用。表格视图正在被填充,所以我知道控制器中的其他tableView方法正在被调用。

有没有人知道我到底搞砸了什么才让这一切发生的?


当前回答

我也有同样的问题,

原因是使用UITapGestureRecognizer。我想让键盘在我点击其他地方时消失。我意识到这覆盖了所有的点击动作,这就是为什么,didSelectRowAtIndexPath函数没有被调用。

当我评论与UITapGestureRecognizer相关的行时,它起作用了。此外,你可以检查UITapGestureRecognizer选择器的功能,如果点击的是UITableViewCell或不是。

其他回答

即使另一个答案已经被接受,我将为观察这个问题的人补充一个可能的问题和解决方案:

If you have automatic reference counting (ARC) turned on, you may find that even after assigning your controller as a delegate of the view, the view's messages to the controller are not being received because ARC is deleting the controller. Apparently the UITableView's delegate pointer does not count as a reference for the ARC, so if that is the only reference to it, the controller will be dealloc'd. You can verify whether or not this is happening by implementing the dealloc method on the controller and setting a breakpoint or NSLog call there.

解决方案是在其他地方使用强引用跟踪控制器,直到确定不再需要它为止。

以防你和我有同样的问题 显然,如果tableView处于编辑模式,这个方法不会被调用。你必须设置allowsSelectionDuringEditing为true。

通过这个问题:编辑时,' UITableView '不调用didSelectRowAtIndexPath ??

都是很好的答案,但还有一个需要注意……

(特别是当以编程方式创建UITableView时)

确保tableView可以通过设置[tableView setAllowsSelection:YES]来响应选择;或者删除任何将其设置为NO的行。

我也有同样的问题,

原因是使用UITapGestureRecognizer。我想让键盘在我点击其他地方时消失。我意识到这覆盖了所有的点击动作,这就是为什么,didSelectRowAtIndexPath函数没有被调用。

当我评论与UITapGestureRecognizer相关的行时,它起作用了。此外,你可以检查UITapGestureRecognizer选择器的功能,如果点击的是UITableViewCell或不是。

好的,在这里更新,因为我刚刚遇到了这个问题,我的问题与这里发现的略有不同。

我在IB中查看,看到我的委托被设置,但它被错误地设置为视图,而不是文件的所有者(右键单击表视图,查看委托指向的位置)。

希望这能帮助到别人