xcode CollectionViewController scrollToItemAtIndexPath不工作

我已经创建了一个CollectionView控件并填充了图像。现在我想在开始时滚动到特定索引的项目。我已经尝试了scrollToItemAtIndexPath,如下所示:

[self.myFullScreenCollectionView scrollToItemAtIndexPath:indexPath 
atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];

但是,我得到以下异常。任何人都可以指导我去哪里我错了。

2013-02-20 02:32:45.219 ControlViewCollection1[1727:c07] *** Assertion failure in 
-[UICollectionViewData layoutAttributesForItemAtIndexPath:], /SourceCache/UIKit_Sim/UIKit-2380.17
/UICollectionViewData.m:485 2013-02-20 02:32:45.221 ControlViewCollection1[1727:c07] must return a 
UICollectionViewLayoutAttributes instance from -layoutAttributesForItemAtIndexPath: for path 
<NSIndexPath 0x800abe0> 2 indexes [0, 4]
无论是错误还是功能,UIKit会在scrollToItemAtIndexPath:atScrollPosition:animated被调用之前调用UICollectionView已经布局了它的子视图。

作为解决方法,将滚动调用移动到视图控制器生命周期中的一个位置,您可以确定已将其布局布局,如下所示:

@implementation CollectionViewControllerSubclass

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    // scrolling here doesn't work (results in your assertion failure)
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    NSIndexPath *indexPath = // compute some index path

    // scrolling here does work
    [self.collectionView scrollToItemAtIndexPath:indexPath
                                atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
                                        animated:YES];
}

@end

至少,错误信息应该更有帮助。我开了一个rdar://13416281;请欺骗。

http://stackoverflow.com/questions/14977896/xcode-collectionviewcontroller-scrolltoitematindexpath-not-working

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:xcode CollectionViewController scrollToItemAtIndexPath不工作