我正在编写一个带有tab视图中的表视图的iOS应用程序。在我的UITableViewController中,我实现了-tableView:didSelectRowAtIndexPath:,但当我在运行时选择一行时,该方法没有被调用。表格视图正在被填充,所以我知道控制器中的其他tableView方法正在被调用。
有没有人知道我到底搞砸了什么才让这一切发生的?
我正在编写一个带有tab视图中的表视图的iOS应用程序。在我的UITableViewController中,我实现了-tableView:didSelectRowAtIndexPath:,但当我在运行时选择一行时,该方法没有被调用。表格视图正在被填充,所以我知道控制器中的其他tableView方法正在被调用。
有没有人知道我到底搞砸了什么才让这一切发生的?
当前回答
即使另一个答案已经被接受,我将为观察这个问题的人补充一个可能的问题和解决方案:
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.
解决方案是在其他地方使用强引用跟踪控制器,直到确定不再需要它为止。
其他回答
我在我的表视图上放了一个UITapGestureRecognizer来消除键盘,这阻止了didSelectRowAtIndexPath:被调用。希望它能帮助到别人。
这些答案对我都没用。大约一个小时后,我发现了一些非常阴险的事情:
我在另一个表格视图的单元格中有一个表格视图。我决定创建一个包含内部表视图的封闭视图。我将这个视图命名为contentView并在xib中连接它。
UITableViewCell已经有一个contentView并对它做了一些奇怪的事情。当我将属性重命名为mainContentView并将视图重新连接到这个重命名的属性时,问题自行解决了。
我自己也有这个问题。我已经在IB中构建了视图的骨架(只是一个视图和一个TableView),委托没有设置。我把它连接到文件的所有者,它就像一个魅力。::保存::
在这种情况下,我遇到了两件事。
你可能忘了实现UITableViewDelegate协议,或者在你的类和你的表视图之间没有委托出口。 你可能有一个UIView在你的行里面,它是一个第一响应器,拿走你的点击。比如UIButton或类似的东西。
另一件可能导致问题的事情是不被选择的选择类型:
正常选择应为单选择,不应为无选择。
要以编程方式做到这一点,请执行:
tableView.allowsSelection = YES