Empty all cells from UICollectionView

2019-06-08 13:12发布

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?

2条回答
对你真心纯属浪费
2楼-- · 2019-06-08 13:33

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.

查看更多
可以哭但决不认输i
3楼-- · 2019-06-08 13:39

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.

查看更多
登录 后发表回答