好的基本概述我有一个UICollectionView,我需要支持通过performBatchUpdates:方法添加和删除项目.如果我使用标准的UICollectionViewFlowLayout,它工作正常.
但是,当我尝试使用由UIDynamicAnimator驱动的UICollectionViewFlowLayout时,一旦我调用performBatchChanges,我就会遇到崩溃.
在我的自定义UICollectionViewFlowLayout类中,prepareForCollectionViewUpdates:方法从未被调用.我使用的自定义UICollectionViewFlowLayout基于this sample.
崩溃后的控制台输出是…
*** 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
有任何想法吗?
解决方法
尝试
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewLayoutAttributes *layoutAttributes = [self.dynamicAnimator layoutAttributesForCellAtIndexPath:indexPath]; if(!layoutAttributes) { layoutAttributes = [super layoutAttributesForItemAtIndexPath:indexPath]; } return layoutAttributes; }
当执行performBatchUpdates时,[self.dynamicAnimator layoutAttributesForCellAtIndexPath:如果由update创建的单元格不可见,则返回nil.所以只是返回超级(也许UICollectionViewFlowLayout)’现在的layoutAttributes.当要显示的单元格时,UIDynamicAnimator将为您做这项工作.