简体   繁体   English

UICollectionView,单元格选择不起作用,单元格滚动正常

[英]UICollectionView, Cell selection not working, cell scrolls fine

So I have a uicollectionview, which resides inside a view controller, in this VC I have some buttons and other controls, and I load data into the CollectionView, I cannot detect the click on the collectionview, but it responds to scrolls fine. 所以我有一个uicollectionview,它位于一个视图控制器内,在这个VC中我有一些按钮和其他控件,我将数据加载到CollectionView中,我无法检测到集合视图上的点击,但它对滚动响应很好。 All I can think of is that sometimes I enable userinteraction on all subviews in my view ( including the collectionview) , and this works, in the sense that no controls work, and then based on another condition I enable activity. 我能想到的是,有时我在视图中的所有子视图(包括集合视图)上启用了用户交互,并且这在没有控件工作的意义上有效,然后基于另一个条件我启用了活动。

this is an example of the code 这是代码的一个例子

if (_isMenuVisible) {
  for (UIView *subview in [self.actualView subviews]) {
     if([subview isMemberOfClass:[UIButton class]] || [subview isMemberOfClass:[UILabel class]] ||   [subview isMemberOfClass:[UIView class]]) {
        subview.userInteractionEnabled = _isMenuVisible;
     }
     else {
        subview.userInteractionEnabled = !_isMenuVisible;
     }
  }
}
else {
  for (UIView *subview in [self.actualView subviews]) {
        subview.userInteractionEnabled = !_isMenuVisible;
  }
}

Could this affect the behavior? 这会影响行为吗?

Make sure your ViewController adopts the UICollectionViewDelegate protocol. 确保您的ViewController采用UICollectionViewDelegate协议。 Then, as mentioned above, simply override the collectionView:didSelectItemAtIndexPath: to decide what you want to do when a certain cell is clicked. 然后,如上所述,只需覆盖collectionView:didSelectItemAtIndexPath:来决定单击某个单元格时要执行的操作。

Use the indexPath to determine the row or section of the cell by accessing its row or section property. 使用indexPath通过访问其行或节属性来确定单元格的行或节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM