美文网首页
UITableView和UICollectionView的cel

UITableView和UICollectionView的cel

作者: SimonYang966 | 来源:发表于2019-03-26 13:54 被阅读0次

    两种情形的解决方案如下:

    Scenario1 : UITableView中的cell点击实现

    /*1.遵守UIGestureRecognizerDelegate的协议*/
    @interface ****ViewController ()<UIGestureRecognizerDelegate>
    @end
    
    /*2.Create GR对象,并且当前controller成为代理*/
    -(void)viewDidLoad{
        UITapGestureRecognizer *tapGr = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClicked:)];
        tapGr.delegate = self;
        [self.view addGestureRecognizer:tapGr];
    }
    
    /*3.实现这个代理方法*/
    #pragma mark Delegate for the gestureRecognizer
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
        if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
            return NO;
        }
        return YES;
    }
    
    

    Scenario 2 : UICollectionView中的cell点击失效

    /*1.遵守UIGestureRecognizerDelegate的协议*/
    @interface ****ViewController ()<UIGestureRecognizerDelegate>
    @end
    
    /*2.Create GR对象,并且当前controller成为代理*/
    -(void)viewDidLoad{
        UITapGestureRecognizer *tapGr = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClicked:)];
        tapGr.delegate = self;
        [self.view addGestureRecognizer:tapGr];
    }
    
    /*3.实现这个代理方法*/
    #pragma mark Delegate for the gestureRecognizer
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
        //注意传入的参数是当前的CollectionView对象
        if ([touch.view isDescendantOfView:self.currentCollectionView]) {
            return NO;
        }
        return YES;
    }
    
    

    两者的区别就是最后手势代理方法的实现。

    相关文章

      网友评论

          本文标题:UITableView和UICollectionView的cel

          本文链接:https://www.haomeiwen.com/subject/fxygvqtx.html