How get UITableView IndexPath from UITableView iphone?

后端 未结 4 939
粉色の甜心
粉色の甜心 2021-01-22 22:20

In my iPhone app I have one messaging screen. I have added UITapGestureRecognizer on the UIViewController and also I have a UITableview on

4条回答
  •  有刺的猬
    2021-01-22 23:03

    Assuming you want the tap gesture to work everywhere except over the tableview, you could subclass the tap gesture recognizer, creating a recognizer that will ignore any subviews included in an array of excludedViews, preventing them from generating a successful gesture (thus passing it on to didSelectRowAtIndexPath or whatever):

    #import 
    
    @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
    

    And then, when you want to use it, just specify what controls you want to exclude:

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

提交回复
热议问题