gpt4 book ai didi

ios - Collection View performBatchUpdates 崩溃

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:21:34 26 4
gpt4 key购买 nike

我正在尝试使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com