本文介绍了在 UICollectionView 上调用 reloadData 后 contentSize 未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道为什么在 UICollectionView 上调用 reloadData 后 contentSize 没有立即更新?

Does anyone know why contentSize is not updated immediately after reloadData is called on UICollectionView?

如果您需要知道 contentSize,我发现的最佳解决方法如下:

If you need to know the contentSize the best work around I've found is the following:

[_collectionView reloadData];

double delayInSeconds = 0.0001;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(DISPATCH_TIME_NOW, dispatch_get_main_queue(), ^(void)
    {
        // TODO: Whatever it is you want to do now that you know the contentSize.
    });

显然,这是一个相当脆弱的 hack,它对 Apple 的实现做出假设,但到目前为止,它已被证明非常可靠.

Obviously this is a fairly brittle hack that makes assumptions on Apple's implementation but so far it has proven to work quite reliably.

是否有人对为什么会发生这种情况有任何其他解决方法或知识?我正在辩论提交雷达,因为我不明白为什么他们不能在同一个运行循环中计算 contentSize.这就是 UITableView 在其整个实现中的工作方式.

Does anyone have any other workarounds or knowledge on why this happens? I'm debating submitting a radar because this I can't understand why they cannot calculate the contentSize in the same run loop. That is how UITableView has worked for its entire implementation.

这个问题用于引用块内的 setContentOffset 方法,因为我想在我的应用程序中滚动集合视图.我删除了方法调用,因为人们的答案集中在为什么我没有在 contentSize 没有更新的原因中使用 scrollToItemAtIndexPath.

This question use to reference the setContentOffset method inside the block because I want to scroll the collection view in my app. I've removed the method call because peoples' answers focused on why wasn't I using scrollToItemAtIndexPath inside of why contentSize is not being updated.

推荐答案

要获取reload后的内容大小,尝试调用布局对象的collectionViewContentSize.这个对我有用.

To get the content size after reload, try to call collectionViewContentSize of the layout object. It works for me.

这篇关于在 UICollectionView 上调用 reloadData 后 contentSize 未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 10:04