使用来自用户库的图像创建一个带有 UICollectionVIew 的viewcontroller。
加载资产并从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
答案 0 :(得分:1)
我认为问题源于传递给enumerateGroupsWithTypes
的块未在UI线程上执行的事实。尝试使用performSelectorOnMainThread
执行更新UI的代码,如此问题中所述: