我正在开发一个具有UICollectionViewController的应用程序,该应用程序在某些难以重现的神秘情况下崩溃.崩溃的日志如下所示:
*** Assertion failure in -[UICollectionViewData validateLayoutInRect:], /SourceCache/UIKit_Sim/UIKit-3318.16.14/UICollectionViewData.m:417
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView received layout attributes for a cell with an index path that does not exist: <NSIndexPath: 0xc000000000008016> {length = 2, path = 0 - 1}'
Run Code Online (Sandbox Code Playgroud)
一旦我们切换到iOS 8 SDK,这样的崩溃似乎才开始在我们的代码中出现.
为什么会这样?
注意:我已经知道问题的答案是什么,但是我发现很少有关于Stack Overflow和网络其他部分崩溃的信息.我将在下面发布答案.这个错误让我的同事和我三天追踪,所以希望这篇文章能为其他人节省很多时间和挫折.我已向Apple提交此错误.
UICollectionViewData validateLayoutInRectiOS7上的断言失败.
我试图UICollectionView使用for循环逐个删除所有项目; 我在下面发布了我的代码.我删除了UICollectionView使用的项目deleteItemsAtIndexPaths.它在iOS6上完美运行,但在iOS7中崩溃,但有以下异常:
UICollectionViewData中的断言失败validateLayoutInRect
我collectionArray从那时开始self.collectionView逐个删除对象indexPath.当我删除第4个对象时,它会Assertion failure在iOS7上引发.我在这里使用performBatchUpdates.
请帮我在iOS7中获得正确的结果.分享正确的代码.提前致谢.
try {
for (int i=count-1; i>=0; i--) {
[self.collectionView performBatchUpdates:^(void){
[collectionArray removeObjectAtIndex:i]; // First delete the item from you model
[self.collectionView deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:i inSection:0]]];
} completion:nil];
[self.collectionView reloadData];
}
}
@catch (NSException *exception) {
}
@finally {
}
Run Code Online (Sandbox Code Playgroud) 我有一个视图设置有两个UICollectionViews.这些视图中的每一个都有一个支持不同大小的数组.collection1由array1支持,collection2由array2支持.问题是,从numberOfItemsInSection为collection1返回的任何数字都应用于两个集合视图.
例如,如果array1的大小为4,array2的大小为5,则两个集合都将显示4个元素.如果array1的大小为5而array2的大小为4,那么当我一直滚动collection2时,它会调用cellForItemAtIndexPath,itemIndex为5,这样我就得到了一个NSRangeException.
如何让每个collectionView使用它自己的大小?
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section;
{
if(view == self.colleciton1){
return self.array1.count;
} else if (view == self.collection2){
return self.array2.count;
}
return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
if(cv == self.collection1){
CharacterCell *cell = [cv dequeueReusableCellWithReuseIdentifier:FIRST_CELL_IDENTIFIER forIndexPath:indexPath];
cell.label.text = self.array1[indexPath.item];
return cell;
} else if (cv == self.collection2){
EpisodeCell *cell = [cv dequeueReusableCellWithReuseIdentifier:SECOND_CELL_IDENTIFIER forIndexPath:indexPath];
cell.label.text = self.array2[indexPath.item];
return cell;
}
return nil;
}
Run Code Online (Sandbox Code Playgroud)
我已经包含了一个git repo,其中包含一个说明问题的项目.
git@github.com:civatrix/MultipleCollectionViews.git
我遇到了一个奇怪的崩溃并试图整天修复它.我有一个自定义UICollectionViewLayout,它基本上增加了细胞的重力和碰撞.
实施效果很好!当我尝试使用以下命令删除一个单元格时会出现问题:[self.collectionView performBatchUpdates:].
它给了我以下错误:
2013-12-12 21:15:35.269 APPNAME[97890:70b] *** Assertion failure in -[UICollectionViewData validateLayoutInRect:], /SourceCache/UIKit_Sim/UIKit-2935.58/UICollectionViewData.m:357
2013-12-12 20:55:49.739 APPNAME[97438:70b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView recieved layout attributes for a cell with an index path that does not exist: <NSIndexPath: 0x975d290> {length = 2, path = 0 - 4}'
Run Code Online (Sandbox Code Playgroud)
我的模型处理正确,我可以看到它从中删除项目!
要删除的项的indexPaths正在对象之间正确传递.collectionView更新没有崩溃的唯一时间是我删除它上面的最后一个单元格,否则,崩溃就会发生.
这是我用来删除单元格的代码.
- (void)removeItemAtIndexPath:(NSIndexPath *)itemToRemove completion:(void (^)(void))completion
{
UICollectionViewLayoutAttributes *attributes = [self.dynamicAnimator layoutAttributesForCellAtIndexPath:itemToRemove];
[self.gravityBehaviour removeItem:attributes];
[self.itemBehaviour removeItem:attributes];
[self.collisionBehaviour removeItem:attributes];
[self.collectionView performBatchUpdates:^{
[self.fetchedBeacons removeObjectAtIndex:itemToRemove.row];
[self.collectionView deleteItemsAtIndexPaths:@[itemToRemove]];
} completion:nil];
} …Run Code Online (Sandbox Code Playgroud) objective-c ios uicollectionview uicollectionviewlayout uikit-dynamics