问题描述
在iOS7上的 UICollectionViewData
validateLayoutInRect
中的断言失败。
Assertion Failure in UICollectionViewData
validateLayoutInRect
on iOS7.
我正在尝试使用 for
循环逐个删除所有 UICollectionView
项目;我在下面发布了我的代码。我使用 deleteItemsAtIndexPaths
删除 UICollectionView
项目。它在iOS6上完美运行,但在iOS7中崩溃,但有以下异常:
I am trying to delete all UICollectionView
items, one by one, using a for
loop; I posted my code below. I delete the UICollectionView
items using deleteItemsAtIndexPaths
. It's working perfectly on iOS6, but crashes in iOS7 with this exception:
我从 collectionArray
中删除对象然后 self.collectionView
,一个接一个,使用 indexPath
。当我删除第4个对象时,它会在iOS7上引发断言失败
。我在这里使用 performBatchUpdates
。
I delete the object from collectionArray
then self.collectionView
, one by one, using indexPath
. When I delete the 4th object its raises Assertion failure
on iOS7. Here I am using performBatchUpdates
.
请帮助我在iOS7中获得正确的结果。分享正确的代码。
提前致谢。
Please help me get the proper result in iOS7. Share proper code.Thanks in advance.
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 {
}
推荐答案
我实际上有一次这次崩溃不是因为我在一个部分或项目中返回零部分,但因为我重复使用这样的流布局超过1个集合视图:
I actually got this crash one time not because I was returning zero for a number of sections or items in a section but because I was reusing a flow layout like this for more than 1 collection view:
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
Collection1 = [[UICollectionView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 50.0f) collectionViewLayout:flowLayout];
[Collection1 setDataSource:self];
[Collection1 setDelegate:self];
[self.view addSubview:Collection1];
Collection2 = [[UICollectionView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, self.view.frame.size.height) collectionViewLayout:flowLayout];
Collection2.backgroundColor = [UIColor whiteColor];
相反,如果我为每个UICollectionView创建一个新的流布局,我可以避免这种崩溃。希望这可以帮助某人
Instead if I create a new flow layout for each UICollectionView I avoid this crash. Hopefully that might help someone
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
Collection1 = [[UICollectionView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 50.0f) collectionViewLayout:flowLayout];
[Collection1 setDataSource:self];
[Collection1 setDelegate:self];
[self.view Collection1];
UICollectionViewFlowLayout *flowLayoutVert = [[UICollectionViewFlowLayout alloc] init];
[flowLayoutVert setScrollDirection:UICollectionViewScrollDirectionVertical];
Collection2 = [[UICollectionView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, self.view.frame.size.height) collectionViewLayout:flowLayoutVert];
这篇关于ios7上的UICollectionViewData validateLayoutInRect中的断言失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!