Akhilesh Sharma Akhilesh Sharma - 1 month ago 12
iOS Question

Selecting all the items in UICollectionView iOS, even the cells that are not visible

I am in the middle of creating an application where I have a button on a tool bar which selects all the items inside a collection view.

But the problem that I am facing is, when I tap on the button it selects only those items that are visible on the screen. This is due to the CELL REUSE functionality.

Is there any way that I can select all the cells, even those which are not currently visible to the user?

Thanks
J

Answer

It is not possible to have all cells selected when using cell reuse.

Due to cell reuse the number of actual cells which exist at any moment is a couple more than the number of cells currently visible. i.e. 6 cells visible is about 8 cells existing.

You are able to find out how many visible cells there are with

NSArray *visiblePaths = [self.collectionView indexPathsForVisibleItems];

The solution is to have the selected value stored within the UICollectionView datasource and use that value for when you customise a cell inside cellForItemAtIndexPath