UICollectionView multiple Selection Reusability is

2019-08-20 08:40发布

问题:

I am new to UICollectionview. I have made a simple sample were I can apply multiple selection of image. but I'am getting issue regarding selection, the issue is when I scroll collection view the check mark are added to other cells and the images array count is different at didSelectedItem and didDeselectItem.

Here is the sample code which i have tried Sample Link

回答1:

When you scroll in a CollectionView (or a in TableView for that matter), the cells are reused.
To keep the selection while scrolling you need to keep an array of the selected IndexPaths and update the cells accordingly in your collectionView:cellForItemAtIndexPath:. That means removing the checkmark or setting it if the selected array contains this IndexPath.