简体   繁体   中英

didDeselectItemAtIndexPath function is not being called in programmatically created UICollectionView

I have created a UICollectionView programmatically. Its cells also contain programmatically created UILabel, UIView and UIImageView. The view that contains this collection view has also been created programmatically. I've set its datasource and delegate to self. The userInteractionEnabled property of label, view and image view has also been set to false. The allowsMultipleSelection property of collection view is also set to true.

The problem is that the didSelectItemAtIndexPath function is called whenever I touch the cells. But didDeselectItemAtIndexPath function is never called. I tried to set the cell.selected property to true/false as required but no aid to my problem.

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. I highly advertise you to check this points:

  1. After creation your custom collectionView don't forget to set it delegate property.
  2. Implement this methods in your delegate object. -collectionView:didSelectItemAtIndexPath: or -collectionView:didDeselectItemAtIndexPath: and put there simple NSLog() output or breakpoints
  3. Don't forget to set up UICollectionView property allowsSelection to 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. As you remember, the same as in UITableViewClass
2. UICollectionView property allowsMultiplySelection allows you to switch between collection cells states
3. It doesn't matter where you create you elements. It just need to be setup correctly.

Hope this may help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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