Ios 刷新UICollectionview
是否有人知道在显示集合视图时如何重新加载/刷新UICollectionView?基本上,我正在寻找与UITableview的标准重载数据方法类似的方法。您可以调用:Ios 刷新UICollectionview,ios,objective-c,uicollectionview,reloaddata,Ios,Objective C,Uicollectionview,Reloaddata,是否有人知道在显示集合视图时如何重新加载/刷新UICollectionView?基本上,我正在寻找与UITableview的标准重载数据方法类似的方法。您可以调用: [self.myCollectionView reloadData]; 也可以重新加载各个部分和项目: [self.myCollectionView reloadSections:indexSet]; [self.myCollectionView reloadItemsAtIndexPaths:arrayOfIndexPaths]
[self.myCollectionView reloadData];
也可以重新加载各个部分和项目:
[self.myCollectionView reloadSections:indexSet];
[self.myCollectionView reloadItemsAtIndexPaths:arrayOfIndexPaths];
正确和最好的方法是使用
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
}
}];
所有这些都被预先计算成一个很好的动画。最好的做法是先删除然后再添加所有元素,以避免冲突。也许我有点误解,但UICollectionView实现了重载数据。WFIW我相信人们之所以会遇到这种情况,是因为他们习惯于调用[myTableView reloadData],但对于UICollectionView,您必须调用[myCollectionView.collectionView重载数据][self.myCollectionView重载数据];只调用一次。要重新加载所有数据。虽然这确实会达到预期的结果,但使用重新加载数据会重新绘制所有单元格,并可能影响性能。这不是一个好主意,因为重新加载整个表会导致性能变差,并且会消耗大量精力。您节省了我的时间!谢谢
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
}
}];