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

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


当前回答

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

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

其他回答

都是很好的答案,但还有一个需要注意……

(特别是当以编程方式创建UITableView时)

确保tableView可以通过设置[tableView setAllowsSelection:YES]来响应选择;或者删除任何将其设置为NO的行。

我的问题不在上面。太差劲了。但我想我应该把它列在这里,也许它能帮助到别人。

I have a tableViewController that is my "base" controller and then I create subclasses of this controller. I was writing all my code in the tableView:didSelectRowAtIndexPath routine in the "base" class. Completely forgetting that by default this routine had also been created (albeit with no code that did anything) in all of my subclasses as well. So when I ran my app, it ran the subclass version of the code, did nothing, and made me sad. So of course, once I removed the routine from the subclasses, it used mt "base" class routine and I'm in business.

我知道。别笑。但也许这能帮别人省下我浪费的一小时…

记住在viewDidLoad方法中设置数据源和委托,如下所示:

[self.tableView setDelegate:self];

[self.tableView setDataSource:self];

这可能只是在我的情况下,但我从备份中重新加载了一些文件,一些东西不能工作,包括这个。在做了完全清洁(产品>清洁或Shift +命令+ K)后,它工作了。可能是在预编译的头文件中搞砸了。这对你来说可能不是问题,但值得一试。

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

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

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

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