Looping through all cells in UICollectionView

16,729

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.

Share:
16,729
user1838169
Author by

user1838169

Updated on July 18, 2022

Comments

  • user1838169
    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
    user1838169 about 11 years
    Wow that's brilliant and works like a charm, thanks so much :)
  • Juan González
    Juan González almost 11 years
    Hi, one (silly) question. Does the reloadItemsAtIndexPaths go in the same collectionView:cellForItemAtIndexPath: just after checking the property?
  • fguchelaar
    fguchelaar almost 11 years
    No, 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
    Jon Madison almost 10 years
    To be clear, you'll want to set the property on the object in the collection that serves as the data source for your collectionview.