2013-02-19 58 views
0

伙计们我在视图控制器中创建了收藏视图。我已经编写了委托和数据源方法的代码。现在我想在一个特定的索引处开始视图,可以说2.我已经在viewDidLoad方法中编写了以下代码。但是,它抛出异常并且我的应用在仿真器中终止。xcode UICollectionView在开始时滚动到默认选定项目

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

PLZ帮我解决这个问题? 其迫切

+0

你会得到哪个错误信息?控制台中必须印有什么东西?如果您设置了一个异常断点,那么在错误信息被打印出来之前,您可能需要在调试器中点击一次或两次左右的继续。 – 2013-02-19 08:13:21

回答

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这里是无关紧要的。

2

我一直在努力完成这项工作&现在我通过使用CollectionView的默认方法使其功能完整。

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

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

关键因素是UICollectionViewScrollPositionNone枚举值。

+0

为我工作,但我不得不把它放在viewDidAppear而不是viewDidLoad或viewWillAppear – user3000868 2016-07-30 04:59:08