Modify UICollectionView User Data

In my project, I use UICollectionView to display a grid of icons.

The user can reorder by clicking on a segmented control that calls a selection from the master data using another NSSortDescriptor.

The amount of data is always the same, only ending in different sections / lines:

- (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 does not revitalize the changes, the UICollectionView just appears with new data.

Should I keep track of which cell indexPath was before and after the change, and use [self.collectionView moveItemAtIndexPath: toIndexPath:] to do the animation for the change, or is there a better method?

I haven't gotten much in subclassing collectionViews, so any help would be great ...

Thanks Bill.

+63
ios uicollectionview
Nov 07
source share
7 answers

reloadData does not animate, nor does it do with reliability when placed in a UIView animation block. It wants to be in the execution unit of the UICollecitonView performBatchUpdates, so try something more similar:

 [self.collectionView performBatchUpdates:^{ [self.collectionView reloadData]; } completion:^(BOOL finished) {}]; 
+57
Nov 28 '12 at 17:44
source share

Wrapping -reloadData in -performBatchUpdates: does not seem to cause a one-sector view of the collection for animation.

 [self.collectionView performBatchUpdates:^{ [self.collectionView reloadData]; } completion:nil]; 

However, this code works:

 [self.collectionView performBatchUpdates:^{ [self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]]; } completion:nil]; 
+128
Mar 01 '13 at 19:51
source share

This is what I did to animate the reboot of ALL SECTIONS:

 [self.collectionView reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, self.collectionView.numberOfSections)]]; 

Swift 3

 let range = Range(uncheckedBounds: (0, collectionView.numberOfSections)) let indexSet = IndexSet(integersIn: range) collectionView.reloadSections(indexSet) 
+44
May 10 '13 at 16:50
source share

Rebooting the entire collection view inside the performBatchUpdates:completion: block makes a failed animation for me on the iOS 9 simulator. If you have a specific UICollectionViewCell that you want to delete, or if you have a path to it, you can call deleteItemsAtIndexPaths: in this block. Using deleteItemsAtIndexPaths: it performs smooth and enjoyable animations.

 UICollectionViewCell* cellToDelete = /* ... */; NSIndexPath* indexPathToDelete = /* ... */; [self.collectionView performBatchUpdates:^{ [self.collectionView deleteItemsAtIndexPaths:@[[self.collectionView indexPathForCell:cell]]]; // or... [self.collectionView deleteItemsAtIndexPaths:@[indexPath]]; } completion:nil]; 
+4
Sep 30 '15 at 10:25
source share

The help text says:

Call this method to reload all items in a collection. This leads to the fact that viewing the collection cancels any currently visible elements and redraw them. For efficiency, only viewing the collection is displayed. these cells and additional views that are visible. If collection data is compressed as a result of a reboot, viewing the collection adjusts the scroll offsets accordingly. You should not call this method in the middle of animation blocks, where elements are inserted or deleted. Insertions and deletions automatically cause tabular data to be updated accordingly.

I think the key part is โ€œinvokes a collection view to remove any currently visible elementsโ€. How will this revitalize the movement of objects that it has thrown away?

+2
Apr 19 '13 at 18:20
source share

If you want more control and a custom function, check this , it contains a very detailed explanation of the different ways of animating UICollectionViewCell.

+2
Mar 31 '15 at 19:15
source share

Itโ€™s convenient for fast users -

 collectionView.performBatchUpdates({ self.collectionView.reloadSections(NSIndexSet(index: index)) }, completion: nil) 
-2
Jul 19 '16 at 5:58
source share



All Articles