2

我使用用户库中的图像创建一个带有UICollectionVIew的视图控制器。

加载资产并从 UICollectionView 重新加载数据后,我尝试滚动到 UICollectionView 中的最后一项。

UICollectionView 已填充,但滚动到最后一个对象时收到错误消息。

这是我的代码

void (^assetEnumerate)(ALAsset *result, NSUInteger index, BOOL *stop) = ^(ALAsset* result, NSUInteger index, BOOL *stop){
    if (result != nil){
        [_assets addObject:result];
    }
};

void (^ALAssetsLibraryGroupsEnumerationResultsBlock)(ALAssetsGroup *group, BOOL *stop) = ^(ALAssetsGroup *group, BOOL *stop){
    if(group != nil){
        [group enumerateAssetsUsingBlock:assetEnumerate];
        [_collectionView reloadData];

        //OLD:
        //no mather what is filled in the indexPathForRow, it results in an error
        //[_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionTop animated:NO];

        //NEW: fix!
        dispatch_async(dispatch_get_main_queue(), ^{
            [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:[_collectionView numberOfItemsInSection:0] inSection:0] atScrollPosition:UICollectionViewScrollPositionBottom animated:NO];
        });
    }
};

void (^ALAssetsLibraryAccessFailureBlock)(NSError *error) = ^(NSError *error){
    NSLog(@"ERROR: Couldn't enumerate group");
};

[_library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:ALAssetsLibraryGroupsEnumerationResultsBlock failureBlock:ALAssetsLibraryAccessFailureBlock];

错误:

*** Assertion failure in -[UICollectionViewData layoutAttributesForItemAtIndexPath:], /SourceCache/UIKit_Sim/UIKit-2372/UICollectionViewData.m:485
2012-11-06 00:53:17.388 iBoob[6016:c07] *** 
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'must return a UICollectionViewLayoutAttributes instance from -layoutAttributesForItemAtIndexPath: for path <NSIndexPath 0x968af20> 2 indexes [0, 0]'
*** First throw call stack:
(0x2988012 0x14e7e7e 0x2987e78 0xf7df35 0x95f3f2 0x93794e 0xa7e4 0x3fd46 0x2981e7c 0x2981a16 0x2981925 0x3fb8c 0x33eb53f 0x33fd014 0x33ed7d5 0x292eaf5 0x292df44 0x292de1b 0x20ca7e3 0x20ca668 0x42f65c 0x60cd 0x2ab5)
libc++abi.dylib: terminate called throwing an exception
4

1 回答 1

1

我认为问题源于传递给的块enumerateGroupsWithTypes没有在 UI 线程上执行。尝试使用performSelectorOnMainThread执行更新您的 UI 的代码,如本问题中所述:

如何在此线程中间更新 UI?

于 2012-11-06T06:27:14.950 回答