11

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

但是,当我尝试使用UICollectionViewFlowLayout由 a 驱动的 aUIDynamicAnimator时,我一打电话就会崩溃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

有任何想法吗?

4

1 回答 1

13

尝试

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

当您执行时performBatchUpdates,如果更新创建的单元格不可见,则[self.dynamicAnimator layoutAttributesForCellAtIndexPath: 返回。nil所以现在只返回super(也许UICollectionViewFlowLayoutlayoutAttributes。当单元格即将显示时,UIDynamicAnimator将为您完成这项工作。

于 2014-01-13T08:44:54.383 回答