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

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


当前回答

如果UITapGestureRecognizer出现问题,你可以修复这个:

在故事板:

在Objective-C代码中:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)]; 
[self.view addGestureRecognizer:tap];

[tap setCancelsTouchesInView:NO];

Swift代码:

let tap = UITapGestureRecognizer(target: self, action:Selector("dismissKeyboard"))
view.addGestureRecognizer(tap)

tap.cancelsTouchesInView = false

其他回答

在我的情况下,我犯了一个小错误,我分配:

tableView.isUserInteractionEnabled = false

它应该是:

tableView.isUserInteractionEnabled = true

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

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

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

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

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

以防有人和我犯同样愚蠢的错误

检查您所期望的didSelect的方法名是否会以某种方式意外地获得didDeselect。我花了大约两个小时才发现……

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

从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 }
}