我正在编写一个带有tab视图中的表视图的iOS应用程序。在我的UITableViewController中,我实现了-tableView:didSelectRowAtIndexPath:,但当我在运行时选择一行时,该方法没有被调用。表格视图正在被填充,所以我知道控制器中的其他tableView方法正在被调用。
有没有人知道我到底搞砸了什么才让这一切发生的?
我正在编写一个带有tab视图中的表视图的iOS应用程序。在我的UITableViewController中,我实现了-tableView:didSelectRowAtIndexPath:,但当我在运行时选择一行时,该方法没有被调用。表格视图正在被填充,所以我知道控制器中的其他tableView方法正在被调用。
有没有人知道我到底搞砸了什么才让这一切发生的?
当前回答
在我的案例中,解决方案是在下面的函数中将NO更改为YES。
iOS 9 +
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
其他回答
好的,在这里更新,因为我刚刚遇到了这个问题,我的问题与这里发现的略有不同。
我在IB中查看,看到我的委托被设置,但它被错误地设置为视图,而不是文件的所有者(右键单击表视图,查看委托指向的位置)。
希望这能帮助到别人
如果你在UITableView上添加了一个gestureRecognizer, didSelectRowAtIndexPath将不会被调用。
你需要使用gestureRecognizer委托方法来避免特定视图中的触摸。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([touch.view isDescendantOfView:YourTable]) {
return NO;
}
return YES;
}
如果您有一个自定义单元格,请记住在Xib中(或通过代码)为单元格设置UserInteractionEnabled。
我也遇到过同样的问题。而且很难找到。但在我的代码中 是这样的:
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
return nil;
}
它必须返回indexPath,否则-tableView:didSelectRowAtIndexPath:没有被调用。
我的问题不在上面。太差劲了。但我想我应该把它列在这里,也许它能帮助到别人。
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.
我知道。别笑。但也许这能帮别人省下我浪费的一小时…