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

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


当前回答

另一个疯狂的可能性是:我在iPhone 5S上运行我的应用,我使用了:

- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

就像我过去一直做的那样。

然而,我没有仔细查看我的编译器警告…它说我应该把上面这一行改为:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

64位兼容性!摇动的拳头

当心,万一你开始扯你的头发。

其他回答

一个常见的错误是编写调用selectRowAtIndexPath或deselectRowAtIndexPath的代码,并假设该调用将触发对委托的didSelectRowAtIndexPath的调用。但事实并非如此。

selectRowAtIndexPath和deselectRowAtIndexPath的文档清楚地说明:

"这些方法不会调用委托方法 tableView:willSelectRowAtIndexPath:或tableView:didSelectRowAtIndexPath:,也不会发出通知。"

在我的案例中,解决方案是在下面的函数中将NO更改为YES。

iOS 9 +

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

在我的例子中,问题是将方法声明为private。

这并没有起作用:

private func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

这工作:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

检查- hightforrowatindexpath是否不返回0。

另一种可能性是UITapGestureRecognizer可以吃掉事件,就像这里的情况:https://stackoverflow.com/a/9248827/214070

我没有怀疑这个原因,因为表格单元格仍然会突出显示蓝色,就像水龙头通过一样。