Refreshing a UICollectionview

2019-04-03 08:52发布

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.

3条回答
Juvenile、少年°
2楼-- · 2019-04-03 09:23

You can just call:

[self.myCollectionView reloadData];

Individual sections and items can also be reloaded:

[self.myCollectionView reloadSections:indexSet];
[self.myCollectionView reloadItemsAtIndexPaths:arrayOfIndexPaths];
查看更多
手持菜刀,她持情操
3楼-- · 2019-04-03 09:29

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.

查看更多
成全新的幸福
4楼-- · 2019-04-03 09:45
[self.collectionView reloadData];
查看更多
登录 后发表回答