xcode CollectionViewController scrollToItemAtIndexPath不工作
发布时间:2020-12-15 01:57:48 所属栏目:百科 来源:网络整理
导读:我已经创建了一个CollectionView控件并填充了图像。现在我想在开始时滚动到特定索引的项目。我已经尝试了scrollToItemAtIndexPath,如下所示: [self.myFullScreenCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrol
我已经创建了一个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;请欺骗。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |