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

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


当前回答

我间歇性地有这个问题。有时触摸一个单元格会导致它被选中。有时它不会接收touch事件。

我使用的是ios8中引入的一个叫做自调整单元格的功能。我看到一篇博客文章指出:

当表视图第一次显示时,您可能会发现其中的一些单元格 尺寸不合适。但当你滚动表格视图时,新的 显示的单元格具有正确的行高。为了解决这个问题, 你可以在视图出现后强制重载:

override func viewDidAppear(animated: Bool) {
    tableView.reloadData()
}

这为我解决了问题。即使表视图正确呈现,触摸处理(特别是UITableView的hitTest)似乎受到上述错误的影响。

其他回答

我也遇到过同样的问题。而且很难找到。但在我的代码中 是这样的:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
}

它必须返回indexPath,否则-tableView:didSelectRowAtIndexPath:没有被调用。

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

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

希望这能帮助到别人

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

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.

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

如果你看了这个,那么问题还是没有解决。

我有自定义单元格,其中复选框“用户交互启用”是禁用的。所以,我只要打开它。祝你好运。

我刚遇到这个问题,但它并没有马上发生;在选择了几个单元格之后,它们将停止调用didSelectItemAtIndexPath。我意识到问题是集合视图将allowmultipleselection设置为TRUE。因为单元格从未被取消选择,这阻止了未来调用didSelectItemAtIndexPath。