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

前言

最近在写一个商品的分类页面,导航栏有一个搜索框,点击空白页面,收回键盘,在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点击)