0

在重新加载 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).
4

1 回答 1

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();
            }];

但是,如果您在收藏视图中多次重新加载照片,您会为此感到非常痛苦。

于 2014-05-02T18:46:45.603 回答