我正在编写一个带有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;
}
其他回答
另一个疯狂的可能性是:我在iPhone 5S上运行我的应用,我使用了:
- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
就像我过去一直做的那样。
然而,我没有仔细查看我的编译器警告…它说我应该把上面这一行改为:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
64位兼容性!摇动的拳头
当心,万一你开始扯你的头发。
即使另一个答案已经被接受,我将为观察这个问题的人补充一个可能的问题和解决方案:
If you have automatic reference counting (ARC) turned on, you may find that even after assigning your controller as a delegate of the view, the view's messages to the controller are not being received because ARC is deleting the controller. Apparently the UITableView's delegate pointer does not count as a reference for the ARC, so if that is the only reference to it, the controller will be dealloc'd. You can verify whether or not this is happening by implementing the dealloc method on the controller and setting a breakpoint or NSLog call there.
解决方案是在其他地方使用强引用跟踪控制器,直到确定不再需要它为止。
如果你在UITableView上添加了一个gestureRecognizer, didSelectRowAtIndexPath将不会被调用。
你需要使用gestureRecognizer委托方法来避免特定视图中的触摸。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([touch.view isDescendantOfView:YourTable]) {
return NO;
}
return YES;
}
对此发表我的意见。
我有一个自定义UITableViewCell,有一个按钮覆盖整个单元格,所以当触摸发生时,按钮被选中,而不是单元格。
要么删除按钮,要么在我的情况下,我将按钮上的User interaction Enable设置为false,这样单元格就被选中了。
确保在主线程上调用reloadData。如果你从某种异步方法(例如:某种网络请求)调用该方法,表视图可能会或可能不会正确响应(在某些情况下,应用程序甚至会崩溃)。
使用主线程代码块来执行reloadData调用,如果调用是在其他一些方法块中进行的(你不确定):
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[tableView reloadData];
}];