ios - Collection View performBatchUpdates 崩溃

标签 ios uicollectionview

我正在尝试使用 insertItemsAtIndexPaths 将新项目添加到我的 Collection View 中。我的应用程序在 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];
}

崩溃日志如下:

* Assertion failure in -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:]

当单元格未注册到 Collection View 时,会发生此断言。我正在注册我的手机。

最佳答案

这对我有用: 如果 Collection View 为空,则重新加载其他 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];

    }
}

关于ios - Collection View performBatchUpdates 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25265183/

相关文章:

ios - AWS batchGetItem NSInvalidArgumentException JSONKeyPathsByPropertyKey:无法识别的选择器

ios - 使用字段的输出作为 url

ios - UICollectionView 全屏缩放 UICollectionViewCell

ios - UICollectionView 在单元格外渲染图像

ios - StackView 内部的 CollectionView

ios - 选中时如何删除覆盖 UITabBarItem 的蓝色方 block ?

iphone - UITableView 插入旧数据

ios - 在警报 View 之前键盘不会被关闭

ios - 在单元格的 ImageView 中设置图像时的 CollectionView NSInvalidArgument

ios - 调整包含 UITextView 的 UICollectionViewCell 以适应 contentSize