我正在编写一个带有tab视图中的表视图的iOS应用程序。在我的UITableViewController中,我实现了-tableView:didSelectRowAtIndexPath:,但当我在运行时选择一行时,该方法没有被调用。表格视图正在被填充,所以我知道控制器中的其他tableView方法正在被调用。
有没有人知道我到底搞砸了什么才让这一切发生的?
我正在编写一个带有tab视图中的表视图的iOS应用程序。在我的UITableViewController中,我实现了-tableView:didSelectRowAtIndexPath:,但当我在运行时选择一行时,该方法没有被调用。表格视图正在被填充,所以我知道控制器中的其他tableView方法正在被调用。
有没有人知道我到底搞砸了什么才让这一切发生的?
当前回答
您必须选择这些选项
但是如果你想让uitableview在点击时不突出显示,那么你应该改变UITableViewCell属性。
选择“选择”的“无”选项,如下所示
其他回答
听起来好像这个类不是表格视图的UITableViewDelegate,尽管UITableViewController应该自动设置。
你是否有机会将委托重置为其他类?
我的问题不在上面。太差劲了。但我想我应该把它列在这里,也许它能帮助到别人。
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.
我知道。别笑。但也许这能帮别人省下我浪费的一小时…
我间歇性地有这个问题。有时触摸一个单元格会导致它被选中。有时它不会接收touch事件。
我使用的是ios8中引入的一个叫做自调整单元格的功能。我看到一篇博客文章指出:
当表视图第一次显示时,您可能会发现其中的一些单元格 尺寸不合适。但当你滚动表格视图时,新的 显示的单元格具有正确的行高。为了解决这个问题, 你可以在视图出现后强制重载:
override func viewDidAppear(animated: Bool) {
tableView.reloadData()
}
这为我解决了问题。即使表视图正确呈现,触摸处理(特别是UITableView的hitTest)似乎受到上述错误的影响。
都是很好的答案,但还有一个需要注意……
(特别是当以编程方式创建UITableView时)
确保tableView可以通过设置[tableView setAllowsSelection:YES]来响应选择;或者删除任何将其设置为NO的行。
如果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