UICollectionview 即时布局更新

UICollectionview immediate layout update

我有一个小问题,我找不到解决方案。

我试图在 collectionView(collectionView:, cellForItemAtIndexPath indexPath:) 中第一个单元格出队后使 UICollectionViewFlowLayout 的布局无效,以便它在显示给用户之前适合它的内容。我需要这样做,因为我需要访问单元格 ui 元素,这些元素在第一次调用 sizeForItemAtIndexPath 时不可用。

在 iOS 8 中工作正常,但在 iOS 7 中 invalidateLayout 不会立即更新布局,正如 Apple 文档中所述:

It is useful to remember that calling the invalidateLayout method does not begin the layout update process immediately. The method merely marks the layout as being inconsistent with the data and in need of being updated. During the next view update cycle, the collection view checks to see whether its layout is dirty and updates it if it is. In fact, you can call the invalidateLayout method multiple times in quick succession without triggering an immediate layout update each time.

现在我想知道是否有 iOS 7 的解决方法可以立即更新流布局。

reloadData 不是解决方案,因为在 dequeuing 单元格时调用它会弄乱整个集合视图。

我找到了invalidateLayout不立即触发的解决方案:viewDidLayoutSubviews是在collectionview加载后调用的,所以我在这个方法中调用了invalidate方法。有趣的是,这确实有效。