xcode CollectionViewController scrollToItemAtIndexPath 不起作用 [英] xcode CollectionViewController scrollToItemAtIndexPath not working

查看:28
本文介绍了xcode CollectionViewController scrollToItemAtIndexPath 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 CollectionView 控件并用图像填充它.现在我想在开始时滚动到特定索引处的项目.我试过 scrollToItemAtIndexPath 如下:

I have created a CollectionView Control and filled it with images. Now I want to scroll to item at a particular index on start. I have tried out scrollToItemAtIndexPath as follows:

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

但是,我遇到了以下异常.谁能指导我哪里出错了.

However, I am getting following exception. Could anyone guide me on where am I going wrong.

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]

推荐答案

无论是 bug 还是功能,只要 scrollToItemAtIndexPath:atScrollPosition:AnimatedUICollectionView<之前被调用,UIKit 都会抛出这个错误/code> 已经布局了它的子视图.

Whether it's a bug or a feature, UIKit throws this error whenever scrollToItemAtIndexPath:atScrollPosition:Animated is called before UICollectionView has laid out its subviews.

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

As a workaround, move your scrolling invocation to a place in the view controller lifecycle where you're sure it has already computed its layout, like so:

@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;请自重.

At the very least, the error message should probably be more helpful. I've opened a rdar://13416281; please dupe.

这篇关于xcode CollectionViewController scrollToItemAtIndexPath 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆