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

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


当前回答

我有个电话要打。userInteractionEnabled隐藏在一些定制表代码中。

大多数虫子都很蠢,不是吗?

其他回答

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

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

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

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

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

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

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

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

64位兼容性!摇动的拳头

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

我刚刚找到了另一种不调用didSelect方法的方法。在某种程度上,可能是在func声明本身的一些错误中,XCode建议我将@nonobjc添加到我的方法中:

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

这将继续编译没有抱怨,但你永远不会被ui动作调用。

“那是我的两分钱,我想要回我的零钱”

确保你实现了tableView:didSelectRowAtIndexPath而不是tableView:didDeSelectRowAtIndexPath

这让我在很多场合!!

我也有同样的问题,

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

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