Collectionview performBatchUpdates崩溃

我正尝试添加新项目到我的集合视图使用insertItemsAtIndexPaths。 我的应用程序在performBatchupdate崩溃

- (void) addItems { NSArray *newProducts = @[@"1",@"2",@"3",@"4"]; [self.collectionView performBatchUpdates:^{ NSMutableArray *arrayWithIndexPaths = [NSMutableArray array]; for (NSInteger index = self.array.count; index < (self.array.count + newProducts.count); index++) { [arrayWithIndexPaths addObject:[NSIndexPath indexPathForRow:index inSection:0]]; } [self.array addObjectsFromArray:newProducts]; [self.collectionView insertItemsAtIndexPaths:arrayWithIndexPaths]; } completion:nil]; } 

以下是崩溃日志:

*断言失败 – [UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:]

这个声明发生在单元没有注册到collectionview的时候。 我正在注册我的手机。

这工作对我来说:如果集合视图是空重新加载其他insertItems。

 - (void)addItems { NSArray *newProducts = @[@"1",@"2",@"3",@"4"]; NSMutableArray *arrayWithIndexPaths = [NSMutableArray array]; for (NSInteger index = self.array.count; index < (self.array.count + newProducts.count); index++) { [arrayWithIndexPaths addObject:[NSIndexPath indexPathForRow:index inSection:0]]; } if (self.array) { [self.array addObjectsFromArray:newProducts]; [self.collectionView performBatchUpdates:^{ [self.collectionView insertItemsAtIndexPaths:arrayWithIndexPaths]; } completion:nil]; } else { self.array = [[NSMutableArray alloc] initWithArray:newProducts]; [self.collectionView reloadData]; } }