ios7上的UICollectionViewData validateLayoutInRect中的断言失败

use*_*996 20 objective-c ios uicollectionviewlayout ios7 xcode5

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)

Col*_*ris 24

我实际上有一次这次崩溃不是因为我为一个部分中的一些部分或项目返回零,而是因为我正在重复使用这样的流程布局来获得多个集合视图:

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];
Run Code Online (Sandbox Code Playgroud)

相反,如果我为每个UICollectionView创建一个新的流布局,我会避免这种崩溃.希望这可能对某人有所帮助

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];
Run Code Online (Sandbox Code Playgroud)

  • 一个很大的帮助.节省我的时间. (2认同)

dan*_*ang 20

在iOS 10中,您必须禁用prefetchingEnabled:

// Swift
if #available(iOS 10, *) { 
    collectionView.prefetchingEnabled = false 
}

//Obj C
if ([self.collectionView respondsToSelector:@selector(setPrefetchingEnabled:)]) {
    self.collectionView.prefetchingEnabled = false;
}
Run Code Online (Sandbox Code Playgroud)

  • 先生,你是一个救生员.谢谢.当我使用自定义FlowLayout类时,它确实解决了我的问题. (2认同)