iphone - 如何从 UITableView iphone 获取 UITableView IndexPath?

标签 iphone ios uitableview didselectrowatindexpath uitapgesturerecognizer

在我的 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/

相关文章:

iphone - 在 iOS 中绘制 NSAttributedString?

iphone - 是否可以在 iTunes 连接可用日期之前下载 ios 应用程序?

ios - Cocoapods:未在 xcodebuild 上加载 Google SDK

ios - Firebase 动态链接 : How to get the URI scheme from the dynamic link that gets generated?

ios - 从现有的 tableView 数据源和 Controller 创建一个 collectionView

javascript - 如何从跨域 iframe 中访问父窗口的 window.orientation?

ios - Ionic Build iOS 和 Xcode Build 未部署最新的 Ionic 2 Beta 10 构建

html - iOS Safari Mobile 禁用选择输入的上一个和下一个

iphone - objective-c : Accessing next cell of a UITableView

ios - 带有大标题导航的 UITableView UIRefreshControl 不会粘在顶部 - 它会移动