如何从UITableView iphone获取UITableView IndexPath? [英] How get UITableView IndexPath from UITableView iphone?

查看:108
本文介绍了如何从UITableView iphone获取UITableView IndexPath?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的iPhone应用程序中,我有一个消息传递屏幕。我在 UIViewController 上添加了 UITapGestureRecognizer ,还有一个 UITableview 在屏幕上。我想选择 UITableViewCell 但由于 UITapGestureRecognizer <我无法选择 UITableView / code>。当我触摸屏幕时,只调用了点击手势动作,但是没有调用 UITableView 委托 didSelectRowAtIndexPath:。任何人都可以请我帮助我处理点击手势和 UITableView:didSelectRowAtIndexPath:。提前致谢。

In my iPhone app I have one messaging screen. I have added UITapGestureRecognizer on the UIViewController and also I have a UITableview on the screen. I want to select the UITableViewCell but I can't select the UITableView because of UITapGestureRecognizer. When I touch the screen, only the tap gesture action is called but UITableView delegate didSelectRowAtIndexPath: is not called. Could anyone please help me to work on both tap gesture and UITableView:didSelectRowAtIndexPath:. Thanks in advance.

推荐答案

虽然我更喜欢Matt Meyer的建议或我使用自定义手势识别器的其他建议,另一种解决方案,不涉及自定义手势识别器,将您的点击手势识别器识别您是否点击了tableview中的单元格,如果是,则手动调用 didSelectRowAtIndexPath ,例如:

While I prefer Matt Meyer's suggestion or my other suggestion of using a custom gesture recognizer, another solution, not involving custom gesture recognizers, would be to have your tap gesture recognizer identify whether you tapped on a cell in your tableview, and if so, manually invoke didSelectRowAtIndexPath, e.g.:

- (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 ,然后这可能会完成这项工作。另外两种方法(单独的视图或自定义手势识别器)可以保留完整的tableview功能,但是如果您只需要简单的东西并且不需要tableview的其他内置功能,这可能会有效。

This is suboptimal because if you're doing anything sophisticated with your tableview (e.g. in cell editing, custom controls, etc.), you lose that behavior, but if you're just looking to receive the didSelectRowAtIndexPath, then this might do the job. The other two approaches (separate views or the custom gesture recognizer) let you retain the full tableview functionality, but this could work if you just need something simple and you don't need the rest of the tableview's built-in capabilities.

这篇关于如何从UITableView iphone获取UITableView IndexPath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆