问题 将[UICollectionView performBatchUpdates:]与具有UIDynamics的UICollectionViewFlowLayout一起使用


好的基本概述。我有一个 UICollectionView 我需要通过支持添加和删除项目 performBatchUpdates: 方法。如果我使用标准 UICollectionViewFlowLayout,它工作正常。

但是,当我尝试使用一个 UICollectionViewFlowLayout 由...提供动力 UIDynamicAnimator,一打电话,我就崩溃了 performBatchChanges

在我的习惯上 UICollectionViewFlowLayout 上课 prepareForCollectionViewUpdates: 方法永远不会被调用。习俗 UICollectionViewFlowLayout 我使用的是基于 这个样本

崩溃后的控制台输出是......

*** Assertion failure in -[UICollectionViewData layoutAttributesForItemAtIndexPath:], /SourceCache/UIKit/UIKit-2903.23/UICollectionViewData.m:581
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'no UICollectionViewLayoutAttributes instance for -layoutAttributesForItemAtIndexPath: <NSIndexPath: 0xc000000000028096> {length = 2, path = 2 - 5}'
*** First throw call stack:
libc++abi.dylib: terminating with uncaught exception of type NSException

有任何想法吗?


7996
2017-12-07 00:36


起源

你找到了解决方法吗?下面的答案涵盖了当细胞不可见的时候,但是当细胞可见时,这种情况发生在我身上,并且它在刺激中发生随机崩溃,我无法自我复制。我甚至不使用UIDynamicAnimator - aryaxt
答案打击对我有用,这对我来说是正确的解决方案。 - Ross Kimes


答案:


尝试

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewLayoutAttributes *layoutAttributes = [self.dynamicAnimator layoutAttributesForCellAtIndexPath:indexPath];
    if(!layoutAttributes) {
        layoutAttributes = [super layoutAttributesForItemAtIndexPath:indexPath];
    }
    return layoutAttributes;
}

当你执行 performBatchUpdates[self.dynamicAnimator layoutAttributesForCellAtIndexPath:  回报 nil 如果将通过更新创建的单元格不可见。 所以回报吧 super(也许 UICollectionViewFlowLayout)” 小号 layoutAttributes 目前。 当细胞即将展示时, UIDynamicAnimator 会为你做的工作。


13
2018-01-13 08:44



这是一个知识渊博的答案!谢谢!赏金的...... - Fattie
我应该如何使用你的代码,我得到了一个 类似的问题。 - Nikhil Manapure