gpt4 book ai didi

ios - UICollectionView setCollectionViewLayout 错误 validateLayoutInRect

转载 作者:行者123 更新时间:2023-12-01 16:42:31 24 4
gpt4 key购买 nike

我有两个自定义布局的 collectionView,如果发生某些操作,我将从数据源中删除两个项目并尝试设置不同的 Collection View

    [self.tabCollectionView setCollectionViewLayout:self.basicFlowLayout animated:NO];

我收到错误:
*** Assertion failure in -[UICollectionViewData validateLayoutInRect:], /SourceCache/UIKit_Sim/UIKit-2935.137/UICollectionViewData.m:357
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView recieved layout attributes for a cell with an index path that does not exist: <NSIndexPath: 0xbcecd60> {length = 2, path = 0 - 4}'

但是,当我只删除一个项目后这样做时,动画效果很好。 iOS 7上也没有问题。
一整天都在苦苦挣扎,找不到合适的解决方案来解决它。
有任何想法吗?

最佳答案

我认为问题在于您在短时间内对 Collection View 进行了多次更改,在某些情况下使其状态不一致。

尝试将所有插入/删除/更改布局更改分组到 performBatchUpdates:completion: 中称呼:

Discussion

You can use this method in cases where you want to make multiple changes to the collection view in one single animated operation, as opposed to in several separate animations. You might use this method to insert, delete, reload or move cells or use it to change the layout parameters associated with one or more cells. Use the blocked passed in the updates parameter to specify all of the operations you want to perform.

Deletes are processed before inserts in batch operations. This means the indexes for the insertions are processed relative to the indexes of the state before the batch operation, and the indexes for the deletions are processed relative to the indexes of the state after all the insertions in the batch operation.



如上所述,动画不会被抑制,但我猜它们会被“合并”。

至于异常(exception)情况,您必须确保保留数据模型( dataSource 返回的内容)和 Collection View 之间的对应关系:
  • 在调用 deleteItemsAtIndexPaths: 之前,请确保您已经从数据模型中删除了项目。 .
  • 确保在更改布局时不要更改数据模型。

  • 时机可能很棘手,所以你应该再次尝试 performBatchUpdates .

    关于ios - UICollectionView setCollectionViewLayout 错误 validateLayoutInRect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23014414/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com