In my Project I use UICollectionView to display a grid of icons.
The user is able to change the ordering by clicking a segmented control which calling a fetch from core data with different NSSortDescriptor.
The amount of data is always the same, just ending up in different sections / rows:
- (IBAction)sortSegmentedControlChanged:(id)sender {
_fetchedResultsController = nil;
_fetchedResultsController = [self newFetchResultsControllerForSort];
NSError *error;
if (![self.fetchedResultsController performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
[self.collectionView reloadData];
}
The problem is that reloadData doesn't animate the change, UICollectionView just pops with the new data.
Should I keep track in which indexPath a cell was before and after change, and use [self.collectionView moveItemAtIndexPath: toIndexPath:] to perform the animation for the change or there is a better method ?
I didn't get much into subclassing collectionViews so any help will be great...
Thanks, Bill.
For Swift users, if your collectionview only has one section:
As seen on https://stackoverflow.com/a/42001389/4455570
The help text says:
I think the key part is "causes the collection view to discard any currently visible items". How is it going to animate the movement of items it has discarded?
If you want more control and customizable feature check this, it contains a very detailed explanation of various ways of UICollectionViewCell animations.
For swift users this comes handy -
This is what I did to animate reload of ALL SECTIONS:
Swift 3
Wrapping
-reloadData
in-performBatchUpdates:
does not seem to cause a one-section collection view to animate.However, this code works: