23

Does anyone know how to reload/refresh a UICollectionView while the collection view is being displayed? Basically I'm looking for something similar to the standard reloadData method for a UITableview.

Firo
  • 14,960
  • 3
  • 50
  • 73
SNV7
  • 2,403
  • 5
  • 23
  • 36

3 Answers3

51

You can just call:

[self.myCollectionView reloadData];

Individual sections and items can also be reloaded:

[self.myCollectionView reloadSections:indexSet];
[self.myCollectionView reloadItemsAtIndexPaths:arrayOfIndexPaths];
LinusGeffarth
  • 21,607
  • 24
  • 100
  • 152
Firo
  • 14,960
  • 3
  • 50
  • 73
  • 9
    WFIW I believe the reason why people are coming across this is because they're used to calling [myTableView reloadData] but with UICollectionViews you must call [myCollectionView.collectionView reloadData]. – capikaw Jun 12 '13 at 17:08
  • [self.myCollectionView reloadData]; call only once. want to reload all data. – Anand Prakash Apr 12 '17 at 10:50
10
[self.collectionView reloadData];
Lücks
  • 3,294
  • 2
  • 35
  • 52
  • While this will indeed achieve the desired results, using reloadData redraws all the cells and can affect performance. – DrunkenBeard Jul 23 '14 at 13:19
  • Not a good idea, because reloading the full table you get bad perfomance and energy is drawn. – Karsten Nov 11 '16 at 11:33
1

The correct and best way is to use

NSMutableArray *indexPaths = [NSMutableArray array];
//prepare some data
//batchupdate as block
[self.collectionView performBatchUpdates:^{
    //operations like delete
   [self.collectionView deleteSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, count)]];
    [self.collectionView insertItemsAtIndexPaths:indexPaths];
} completion:^(BOOL finished) {
    // call something when ready
    }
}];

and all gets precomputed an nicely animated. The best practice is to first remove and than add all elements, to avoid conflicts.

Karsten
  • 1,658
  • 18
  • 33