美文网首页iOS开发认知
iOS开发 TableView收回键盘,不影响cell点击

iOS开发 TableView收回键盘,不影响cell点击

作者: sunny冲哥 | 来源:发表于2019-04-04 10:20 被阅读0次

    前言

    最近在写一个商品的分类页面,导航栏有一个搜索框,点击空白页面,收回键盘,在tableview添加了手势,成功收回键盘.但是,,,,,,,,,cell点击事件无法响应了,怎么办呢?


    方法一

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
     action:@selector(tap)];
    tap.cancelsTouchesInView = NO; //切记,否则cell不能点击 ,collectionview同样如此
    [tableV addGestureRecognizer:tap];
    

    方法二

    响应连
    添加uitableview分类 重写hitTest:(CGPoint)point withEvent:(UIEvent *)event方法

    -(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
        id view = [super hitTest:point withEvent:event];
        if (![view isKindOfClass:[UITextField class]]) {
            [self endEditing:YES];
            return self;
        }
        return view;
    }
    

    这段代码的意思就是点击的是tableview就结束编辑并且返回tableview本身,这样就不影响了tableview本身的操作,然后点击的是tableview的子视图的时候就返回子视图就行了。

    注: 以上方法同样适用于collectionview


    相关文章

      网友评论

        本文标题:iOS开发 TableView收回键盘,不影响cell点击

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