使用 reloadData 方法重新加载 UICollectionView 会在重新加载数据之前立即返回 [英] Reloading a UICollectionView using reloadData method returns immediately before reloading data

查看:27
本文介绍了使用 reloadData 方法重新加载 UICollectionView 会在重新加载数据之前立即返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道何时重新加载 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆