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

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


当前回答

检查- hightforrowatindexpath是否不返回0。

其他回答

我也有同样的问题,

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

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

听起来好像这个类不是表格视图的UITableViewDelegate,尽管UITableViewController应该自动设置。

你是否有机会将委托重置为其他类?

如果您所单击的单元格被突出显示,但函数仍然没有被调用,请再次检查函数的签名。它应该是这样的:

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

在我的情况下,问题是我有一个UITableViewCell子类,我实现了这两个方法: touchesBegan:withEvent: & touchesEnded:withEvent处理一个花哨的触摸动画。 但是我忘记添加[super touchesBegan: touchwithevent:event];方法ad [super touchesEnded: touchwithevent:event];也通知父单元的触摸。

所以将代码更改为以下解决了我的问题:

-(void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{
    [super touchesBegan:touches withEvent:event];
    //blah blah blah
}

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesEnded:touches withEvent:event];
    //rest of the code
}

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

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