iOS技术交流收藏

UICollectionViewCell 的点击事件无效,无法触

2019-10-12  本文已影响0人  Xie_Liying

关键词:
collectionView:didSelectItemAtIndexPath: not work
collectionView:didSelectItemAtIndexPath: 不起作用

问题排查:
1、检查是否设置了 collectionView 对象的代理 delegate:

self.collectionView.delegate = self;

2、检查协议方法是否写错:
正确的方法是:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {}

是否写成了:

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {}

3、检查当前页面的是否有手势冲突(我遇到的就是手势冲突):
因为当前页面有一个输入框 UITextField,我的控制器的根视图添加了 UITapGestureRecognizer 手势来实现点击页面收起键盘的操作,UITapGestureRecognizer 手势导致 collectionViewCell 对象的点击事件被 UITapGestureRecognizer 手势捕获了,造成 collectionViewCell 无法响应单击事件。

解决方法:实现 UITapGestureRecognizer 手势对象的协议 delegate,并实现方法

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {}

在方法内判断当前点击的位置是否处于 collectionView 对象内,如果是,则返回 NO 以使 UITapGestureRecognizer 手势对象失效:

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    CGPoint point = [gestureRecognizer locationInView:_collectionView];
    BOOL value = CGRectContainsPoint(_collectionView.bounds, point);
    return !value;
}
上一篇 下一篇

猜你喜欢

热点阅读