Empty all cells from UICollectionView

对着背影说爱祢 提交于 2019-12-08 07:39:24

问题


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.



来源:https://stackoverflow.com/questions/13113850/empty-all-cells-from-uicollectionview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!