在我的 iPhone 应用程序中,我有一个消息屏幕。我在 UIViewController
上添加了 UITapGestureRecognizer
并且在屏幕上还有一个 UITableview
。我想选择 UITableViewCell
,但由于 UITapGestureRecognizer
,我无法选择 UITableView
。当我触摸屏幕时,只会调用点击手势操作,但不会调用 UITableView
委托(delegate) didSelectRowAtIndexPath:
。谁能帮我处理点击手势和 UITableView:didSelectRowAtIndexPath:
。提前致谢。
最佳答案
虽然我更喜欢 Matt Meyer 的建议或我的其他使用自定义手势识别器的建议,但不涉及自定义手势识别器的另一种解决方案是让您的点击手势识别器识别您是否点击了 tableview 中的单元格,以及是否因此,手动调用 didSelectRowAtIndexPath
,例如:
- (void)handleTap:(UITapGestureRecognizer *)sender
{
CGPoint location = [sender locationInView:self.view];
if (CGRectContainsPoint([self.view convertRect:self.tableView.frame fromView:self.tableView.superview], location))
{
CGPoint locationInTableview = [self.tableView convertPoint:location fromView:self.view];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:locationInTableview];
if (indexPath)
[self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
return;
}
// otherwise proceed with the rest of your tap handling logic
}
这是次优的,因为如果你正在对你的 tableview 做任何复杂的事情(例如,在单元格编辑、自定义控件等中),你会失去这种行为,但如果你只是想接收 didSelectRowAtIndexPath
,那么这可能会完成这项工作。其他两种方法(单独的 View 或自定义手势识别器)让您保留完整的 tableview 功能,但如果您只需要一些简单的东西而不需要 tableview 的其余内置功能,这可能会起作用。
关于iphone - 如何从 UITableView iphone 获取 UITableView IndexPath?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11935440/