UICollectionView how to deselect all

后端 未结 6 2184
被撕碎了的回忆
被撕碎了的回忆 2021-02-12 23:26

I have a FollowVC and FollowCell Setup with collection View. I can display all the datas correctly into my uIcollection view cell using the following code with no problem.

6条回答
  •  长情又很酷
    2021-02-12 23:36

    Not all of the selected cells may be on screen at the point when you are clearing the selection status, so collectionView.cellForItemAtIndexPath(indexPath) may return nil. Since you have a force downcast you will get an exception in this case.

    You need to modify your code to handle the potential nil condition but you can also make your code more efficient by using the indexPathsForSelectedItems property of UICollectionView

     let selectedItems = followCollectionView.indexPathsForSelectedItems
     for (indexPath in selectedItems) {
         followCollectionView.deselectItemAtIndexPath(indexPath, animated:true)
         if let cell = followCollectionView.cellForItemAtIndexPath(indexPath) as? FollowCell {
            cell.checkImg.hidden = true
         }
     }
    

提交回复
热议问题