【问题标题】:How get UITableView IndexPath from UITableView iphone?如何从 UITableView iphone 获取 UITableView IndexPath?
【发布时间】:2012-08-13 13:38:37
【问题描述】:

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

【问题讨论】:

  • 您的 tableview 是屏幕上唯一的视图,还是层次结构的一部分?
  • 感谢您的回复。不,tableview 和 UIToolbar 作为屏幕中的子视图。
  • 行为应该如何工作? Tapgesturerecognizer 应该做什么以及什么时候做?

标签: iphone ios uitableview didselectrowatindexpath uitapgesturerecognizer


【解决方案1】:

虽然我更喜欢 Matt Meyer 的建议或我的其他使用自定义手势识别器的建议,但另一种不涉及自定义手势识别器的解决方案是让您的点击手势识别器识别您是否点击了表格视图中的单元格,以及如果所以,手动调用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,那么这可能会完成这项工作。其他两种方法(单独的视图或自定义手势识别器)让您保留完整的 tableview 功能,但如果您只需要简单的东西并且不需要 tableview 的其余内置功能,这可能会起作用。

【讨论】:

    【解决方案2】:

    您可以使用 TagGesture 委托:

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
    {
        if ([touch.view isDescendantOfView:yourTableView]) {
            return NO;
        }
    
        return YES;
    }
    

    希望这会有所帮助。

    【讨论】:

    • 如果touch.viewUIViewController 上,我不确定这是否可行。但我认为这个想法是正确的:您绝对可以查看水龙头的位置并将其与 tableview 的框架进行比较以获得CGPoint location = [gestureRecognizer locationInView:self.view];,然后比较if (CGRectContainsPoint(self.tableView.frame, location)) ...
    • @Rob 我会再检查一下。感谢您的信息。
    【解决方案3】:

    一个更简单的方法是拥有两个视图:一个包含您希望点击手势打开的视图,另一个包含表格视图。您可以将 UITapGestureRecognizer 附加到您希望它处理的视图,然后它不会阻止您的 UITableView。

    【讨论】:

      【解决方案4】:

      假设您希望点击手势在除了 tableview 之外的任何地方都可以工作,您可以对点击手势识别器进行子类化,创建一个识别器,该识别器将忽略 excludedViews 数组中包含的任何子视图,从而阻止它们生成成功的手势 (从而将其传递给didSelectRowAtIndexPath 或其他):

      #import <UIKit/UIGestureRecognizerSubclass.h>
      
      @interface MyTapGestureRecognizer : UITapGestureRecognizer
      @property (nonatomic, strong) NSMutableArray *excludedViews;
      @end
      
      @implementation MyTapGestureRecognizer
      
      @synthesize excludedViews = _excludedViews;
      
      - (id)initWithTarget:(id)target action:(SEL)action
      {
          self = [super initWithTarget:target action:action];
          if (self)
          {
              _excludedViews = [[NSMutableArray alloc] init];
          }
      
          return self;
      }
      
      - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
      {
          [super touchesBegan:touches withEvent:event];
      
          CGPoint location = [[touches anyObject] locationInView:self.view];
      
          for (UIView *excludedView in self.excludedViews)
          {
              CGRect frame = [self.view convertRect:excludedView.frame fromView:excludedView.superview];
      
              if (CGRectContainsPoint(frame, location))
                  self.state = UIGestureRecognizerStateFailed;
          }
      }
      
      @end
      

      然后,当您要使用它时,只需指定要排除的控件:

      MyTapGestureRecognizer *tap = [[MyTapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
      [tap.excludedViews addObject:self.tableView];
      [self.view addGestureRecognizer:tap];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-03
        • 1970-01-01
        • 1970-01-01
        • 2013-03-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多