我不时会在重新加载UICollectionview时遇到此错误,并且我认为错误不是指示性的,因为在此调用之前不应该更新collectionview
-(void)loadGallery:(void(^)())completion
{
[self enumerateAssetsWithCompletion:^(BOOL success, NSMutableArray *assets) {
if (success)
{
self.photos = [assets mutableCopy];
@try {
[self.collectionView performBatchUpdates:^{
[self.collectionView reloadData];
} completion:^(BOOL finished) {
completion();
}];
}
@catch (NSException *exception) {
DLog(@"DEBUG: failure to batch update. %@", exception.description);
}
}
}];
}
- (void)enumerateAssetsWithCompletion:(void(^)(BOOL success,NSMutableArray *assets))completionBlock {
ALAssetsLibrary* al = [[self class] sharedLibrary];
__block NSMutableArray* mutableAssets = [NSMutableArray new];
[al enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup* group, BOOL* stop) {
if (group == nil) {
//self.groups = [mutableGroups copy];
if (completionBlock) {
completionBlock(YES,mutableAssets);
}
}
else {
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if (result)
{
[mutableAssets addObject:result];
}
}];
}
} failureBlock:^(NSError* error) {
ELog(@"Failed to enumerate groups. Error: %@.", error);
if (completionBlock)
completionBlock(NO,nil);
}];
}
错误:
DEBUG: failure to batch update.
Invalid update: invalid number of items in section 0.
The number of items contained in an existing section after the update
(352) must be equal to the number of items contained in that section before the update (0),
plus or minus the number of items inserted or deleted from that section
(0 inserted, 0 deleted)
and plus or minus the number of items moved into or out of that section
(0 moved in, 0 moved out).
答案 0 :(得分:0)
在批量更新块中,您必须执行单元格的插入,删除,重新加载或移动。
只是在批量更新块中重新加载集合视图是不正确的,因为您没有插入任何内容,并且在批量更新之后,集合视图项的新计数比之前更大。
因此,您应该只是重新加载您的集合视图而不进行批量更新,或者在块中插入352个项目。
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, self.photos.count)];
[self.collectionView performBatchUpdates:^{
[indexSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
[self.collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:idx inSection:0]]];
}];
} completion:^(BOOL finished) {
completion();
}];
但是,如果您在收藏夹视图中多次重新加载照片,这将会让您感到非常痛苦。