20

I'd like to reload UICollectionView with some interesting animation. In UITableView there's a method called reloadSections:withRowAnimation. But in UICollectionView there's just reloadSections.

How can I customize reloadSections animation? I definitely saw this in apps on the App Store.

Sergey
  • 39,828
  • 24
  • 80
  • 122
  • possible duplicate of [UICollectionView animate data change](http://stackoverflow.com/questions/13272315/uicollectionview-animate-data-change) – Vinzzz Oct 09 '13 at 13:27

3 Answers3

30

Just do it like that:

[self.collectionView performBatchUpdates:^{
    [self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
} completion:nil];
Fab1n
  • 1,665
  • 14
  • 28
  • Then you have to dive deeper and write your own ```UICollectionViewLayout```. You cannot compare it with UITableView, because UICollectionView is not only ment to display data in 1D (like UITableView) or 2D (like with UICollectionViewFlowLayout). You are not limited to anything and so there are no trivial animations other than fading built in. It is not possible for custom layouts to use different slide-from-top/bottom/middle animations, because the animations cannot be defined globally!!! – Fab1n Oct 11 '13 at 07:57
26

Swift 3 version:

collectionView.performBatchUpdates({
    let indexSet = IndexSet(integer: 0)
    self.collectionView.reloadSections(indexSet)
}, completion: nil)
Mark Bridges
  • 7,606
  • 4
  • 43
  • 59
2

This article here is worth a read: http://victorlin.me/posts/2016/04/29/uicollectionview-invalid-number-of-items-crash-issue

TLDR:

So it turned out performBatchUpdates calls collectionView(:numberOfItemsInSection:) first before call the given closure to know the item numbers. Next, it calls the closure, and eventually, it calls collectionView(:numberOfItemsInSection:) again to check the number. And here is where the assertion exception thrown.

cldrr
  • 1,228
  • 14
  • 22