xcode UICollectionView在开始时滚动到默认选定项目

时间:2013-02-19 08:05:02

标签: objective-c uicollectionview

我在视图控制器中创建了集合视图。我编写了委托和数据源方法的代码。现在我想在一个特定的索引处启动视图,比如说2.我在viewDidLoad方法中编写了以下代码。但是,它会抛出异常,我的应用程序将在模拟器中终止。

NSIndexPath *a=[NSIndexPath indexPathWithIndex:18];
[self.myFullScreenCollectionView scrollToItemAtIndexPath:a atScrollPosition:10 animated:NO];

2 个答案:

答案 0 :(得分:3)

您需要在以下内容中指定有效的枚举值:

[self.myFullScreenCollectionView scrollToItemAtIndexPath:a atScrollPosition:10 animated:NO];

这里第二个参数需要填充一个属于以下枚举的整数 UICollectionViewScrollPosition

typedef NS_OPTIONS(NSUInteger, UICollectionViewScrollPosition) {
    UICollectionViewScrollPositionNone                 = 0,

    // The vertical positions are mutually exclusive to each other, but are bitwise or-able with the horizontal scroll positions.
    // Combining positions from the same grouping (horizontal or vertical) will result in an NSInvalidArgumentException.
    UICollectionViewScrollPositionTop                  = 1 << 0,
    UICollectionViewScrollPositionCenteredVertically   = 1 << 1,
    UICollectionViewScrollPositionBottom               = 1 << 2,

    // Likewise, the horizontal positions are mutually exclusive to each other.
    UICollectionViewScrollPositionLeft                 = 1 << 3,
    UICollectionViewScrollPositionCenteredHorizontally = 1 << 4,
    UICollectionViewScrollPositionRight                = 1 << 5
};

尝试使用其中一个标准枚举值,这可能会有所帮助。我认为10与此无关。

答案 1 :(得分:2)

我努力完成这项工作&amp;现在我通过使用CollectionView的默认方法使其完全正常运行。

这里是如何将集合视图滚动到相应的项目索引。让我们假设我希望集合视图滚动到索引3处的项目,这是代码。

[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:3 inSection:0]
                            atScrollPosition:UICollectionViewScrollPositionNone
                                    animated:NO];

关键因素是UICollectionViewScrollPositionNone枚举值。