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
  • 15,448
  • 3
  • 54
  • 74
SNV7
  • 2,563
  • 5
  • 25
  • 37

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
  • 27,197
  • 29
  • 120
  • 174
Firo
  • 15,448
  • 3
  • 54
  • 74
  • 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
11
[self.collectionView reloadData];
Lücks
  • 3,806
  • 2
  • 40
  • 54
  • 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,869
  • 22
  • 38