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

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


当前回答

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

iOS 9 +

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

其他回答

我有这个问题,当我的TableViewController的类名在接口生成器中设置不正确时。

以防你和我有同样的问题 显然,如果tableView处于编辑模式,这个方法不会被调用。你必须设置allowsSelectionDuringEditing为true。

通过这个问题:编辑时,' UITableView '不调用didSelectRowAtIndexPath ??

我也有同样的问题,

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

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

我不确定是否有人滚动到足够远的地方看到这个答案,但由于这是关于这个主题最受欢迎的问题,而答案并不在那里,我将添加它:

从Xcode 9 / Swift 4开始,所有Objective-C方法都应该标记为@objc。编译器做了一个合理的工作,识别它应该应用在哪里,但它不计算继承。例如:

class Delegate: NSObject, UITableViewDelegate {
}

class SuperDelegate: Delegate {
    override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { return indexPath }
}

这将不会产生任何警告、崩溃或构建错误。但是你的行不会被调用,直到你添加@objc:

@objc class SuperDelegate: Delegate {
    override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { return indexPath }
}

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

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