Empty all cells from UICollectionView

2019-06-08 13:26发布

问题:

Using iOS6's awesome new UICollectionView how am I able to delete all of the UICollectionViewCell objects in a big loop?

Say I've loaded all my data into it already, I hit refresh, I want to delete everything currently in there, then just call my stuff again.

I've found deleteItemsAtIndexPaths which takes an NSArray, so how can I get all items into it?

回答1:

The proper way of clearing out a UICollectionVew is to simply clear the data source and then reload the collection view.

So if your data source was an array:

self.dataArray = nil;
[self.collectionView reloadData];

Boom, you're cleared out.



回答2:

Turns out I can use deleteSections and pass a NSIndexSet through to it, making a range of 0,0 and it'll delete the one and only section.

NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 0)];
[self.collectionView deleteSections:indexSet];

I could probably just use indexSetWithIndex but when I did my app crashed.