问题描述
有没有人知道为什么在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.
});
显然这是一个相当脆弱的黑客,它对苹果公司的实施进行了假设,但到目前为止它已被证明有效非常可靠。
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方法,因为我想在我的应用程序中滚动集合视图。我已经删除了方法调用,因为人们的回答集中在为什么我不使用scrollToItemAtIndexPath为什么没有更新contentSize。
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.
推荐答案
要在重新加载后获取内容大小,请尝试调用布局对象的 collectionViewContentSize
。这个对我有用。
To get the content size after reload, try to call collectionViewContentSize
of the layout object. It works for me.
这篇关于在UICollectionView上调用reloadData后,不会更新contentSize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!