使用reloadData方法重新加载UICollectionView会在重新加载数据之前立即返回 [英] Reloading a UICollectionView using reloadData method returns immediately before reloading data
问题描述
我需要知道何时完成UICollectionView的重新加载以便随后配置单元格(因为我不是单元格的数据源-否则,它已经做到了...)
I need to know when reloading a UICollectionView has completed in order to configure cells afterwards (because I am not the data source for the cells - other wise would have done it already...)
我尝试了
[self.collectionView reloadData];
[self configure cells]; // BOOM! cells are nil
我也尝试使用
[self.collectionView performBatchUpdates:^{
[self.collectionView reloadData];
} completion:^(BOOL finished) {
// notify that completed and do the configuration now
}];
但是当我重新加载数据时,我崩溃了.
but when I reload the data I am getting crashes.
我如何才能将数据重新加载到集合中,并且仅在完成重新加载后才执行-特定的完成处理程序
How can I reload the data into the collection, and only when it has finished reloading - do a particular completion handler
推荐答案
这是由于在layoutSubviews期间而不是reloadData期间添加的单元格引起的.由于layoutSubviews是在reloadData之后的下一个运行循环过程中执行的,因此您的单元格为空. 尝试这样做:
This is caused by cells being added during layoutSubviews not at reloadData. Since layoutSubviews is performed during next run loop pass after reloadData your cells are empty. Try doing this:
[self.collectionView reloadData];
[self.collectionView layoutIfNeeded];
[self configure cells];
我有类似的问题,并以此方式解决了.
I had similar issue and resolved it this way.
这篇关于使用reloadData方法重新加载UICollectionView会在重新加载数据之前立即返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!