简体   繁体   English

在编程创建的UICollectionView中未调用didDeselectItemAtIndexPath函数

[英]didDeselectItemAtIndexPath function is not being called in programmatically created UICollectionView

I have created a UICollectionView programmatically. 我已经以编程方式创建了UICollectionView。 Its cells also contain programmatically created UILabel, UIView and UIImageView. 它的单元格还包含以编程方式创建的UILabel,UIView和UIImageView。 The view that contains this collection view has also been created programmatically. 包含此集合视图的视图也已通过编程方式创建。 I've set its datasource and delegate to self. 我已经将其数据源设置为self。 The userInteractionEnabled property of label, view and image view has also been set to false. 标签,视图和图像视图的userInteractionEnabled属性也已设置为false。 The allowsMultipleSelection property of collection view is also set to true. 集合视图的allowMultipleSelection属性也设置为true。

The problem is that the didSelectItemAtIndexPath function is called whenever I touch the cells. 问题是,每当我触摸单元格时,就会调用didSelectItemAtIndexPath函数。 But didDeselectItemAtIndexPath function is never called. 但是didDeselectItemAtIndexPath函数从不调用。 I tried to set the cell.selected property to true/false as required but no aid to my problem. 我试图根据需要将cell.selected属性设置为true / false,但对我的问题没有帮助。

Please help!! 请帮忙!! I've spent many hours searching about it but no result!! 我花了很多时间搜索它,但没有结果! Thanks for you responses. 感谢您的回复。

Well, both this functions are called according to UICollectionViewDelegate in object which conforms to it. 好吧,这两个函数都是根据符合它的对象中的UICollectionViewDelegate调用的。 I highly advertise you to check this points: 我强烈建议您检查以下几点:

  1. After creation your custom collectionView don't forget to set it delegate property. 创建自定义collectionView后,不要忘记为其设置委托属性。
  2. Implement this methods in your delegate object. 在您的委托对象中实现此方法。 -collectionView:didSelectItemAtIndexPath: or -collectionView:didDeselectItemAtIndexPath: and put there simple NSLog() output or breakpoints -collectionView:didSelectItemAtIndexPath:-collectionView:didDeselectItemAtIndexPath:并放置简单的NSLog()输出或断点
  3. Don't forget to set up UICollectionView property allowsSelection to YES 不要忘记将UICollectionView属性的allowsSelection设置为YES
  4. Face the result 面对结果

Important notes 重要笔记
1. If you call UICollectionView methods like -selectItemAtIndexPath:animated:scrollPosition: or -deselectItemAtIndexPath:animated: any of this action will not be triggered nor notification will be received. 1.如果调用UICollectionView方法,例如-selectItemAtIndexPath:animated:scrollPosition:-deselectItemAtIndexPath:animated: 则不会触发任何此操作, 也不会收到通知。 As you remember, the same as in UITableViewClass 您还记得,它与UITableViewClass中的一样
2. UICollectionView property allowsMultiplySelection allows you to switch between collection cells states 2. UICollectionView属性allowsMultiplySelection允许您在收集单元状态之间切换
3. It doesn't matter where you create you elements. 3.在哪里创建元素都没有关系。 It just need to be setup correctly. 只需正确设置即可。

Hope this may help you. 希望这对您有帮助。

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

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