Looping through all cells in UICollectionView
I would go about this in a different way, probably. If you want the cells to animate, you could set a property shouldAnimate = YES
. Then in your collectionView:cellForItemAtIndexPath:
check that property and apply the animation if needed (or remove it).
After setting the property, reload only the visibleCells: [collectionView reloadItemsAtIndexPaths:collectionView.indexPathsForVisibleItems]
.
Now, since the animation is provided when a cell is requested through the Datasource-Protocol, you also get the animation when you scroll.
user1838169
Updated on July 18, 2022Comments
-
user1838169 almost 2 years
I have a
UICollectionView
and i want to add animation to each cell.Currently I'm using
for(UICollectionView *cell in collectionView.visibleCells){ //add animation to cell here }
But that only applies the animation to the visible cells and as soon as i scroll down and the cell is no longer visible the animation stops.
How do i loop through all the cells in the
UICollectionView
? -
user1838169 about 11 yearsWow that's brilliant and works like a charm, thanks so much :)
-
Juan González almost 11 yearsHi, one (silly) question. Does the reloadItemsAtIndexPaths go in the same collectionView:cellForItemAtIndexPath: just after checking the property?
-
fguchelaar almost 11 yearsNo, put it in the handler for a button-press for example. Placing it in a
collectionView:cellForItemAtIndexPath:
would put your code in a loop. not recommended. -
Jon Madison almost 10 yearsTo be clear, you'll want to set the property on the object in the collection that serves as the data source for your collectionview.