简体   繁体   English

单元格插入时UICollectionView崩溃

[英]UICollectionView crash on cell insertion

I have a UICollectionView with a footer supplementary view. 我有一个带有页脚补充视图的UICollectionView。 The way the collectionView works, it is possible to push the supplementary view offscreen when a new cell is inserted. 通过collectionView的工作方式,可以在插入新单元格时将补充视图推离屏幕。 The cells can have different horizontal sizing. 单元可以具有不同的水平尺寸。

If two large cells are inserted and the supplementary view is pushed off of the screen, inserting a small cell between the two larger cells causes a crash. 如果插入了两个大单元格,并且辅助视图从屏幕上移开,则在两个大单元格之间插入一个小单元格会导致崩溃。

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
reason: 'the view returned from -collectionView:viewForSupplementaryElementOfKind:
atIndexPath (UICollectionElementKindCell,<NSIndexPath: 0xc00000000000000e> 
{length = 1, path = 0}) was not retrieved by calling 
-dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath: or is nil ((null))'

I have determined that the crash occurs because the supplementary view is nil. 我确定发生崩溃是因为补充视图为nil。 It is nil because in 这是零,因为在

 - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView 
   viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

I check to see what kind of element has been handed to the method. 我检查一下将哪种元素传递给该方法。 When the app crashes, the kind is UICollectionElementKindCell, for which I have no valid response. 当应用程序崩溃时,类型为UICollectionElementKindCell,对此我没有有效的响应。

So, my question is twofold : 所以,我的问题是双重的:
1) Why is viewForSupplementaryElementOfKind being called with a kind that is not associated with my supplementary view? 1)为什么用与我的补充视图不相关的一种方法调用viewForSupplementaryElementOfKind? I expect that the UICollectionView should only try to get supplementary view information from this method. 我希望UICollectionView应该仅尝试从此方法获取补充视图信息。

2) How can this crash be avoided? 2)如何避免这种崩溃?

It is unclear why, but this crash was resolved by overriding the method 目前尚不清楚原因,但是此崩溃已通过覆盖方法解决了

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds

and returning true as well as removing the call to invalidateLayout that was in the insertion methods. 并返回true,并删除对插入方法中的invalidateLayout的调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM