Reputation: 597
I have created collection view in view controller. I have written code for delegate and datasource methods. Now I want to start the view at a particular index, lets say 2.I have written following code in viewDidLoad method. However, it is throwing exception and my app is getting terminated in the emulator.
NSIndexPath *a=[NSIndexPath indexPathWithIndex:18];
[self.myFullScreenCollectionView scrollToItemAtIndexPath:a atScrollPosition:10 animated:NO];
Upvotes: 0
Views: 3388
Reputation: 152
You need to assign a valid enum value in following:
[self.myFullScreenCollectionView scrollToItemAtIndexPath:a atScrollPosition:10 animated:NO];
Here the second parameter needs to be filled with an integer that belongs to following enum 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
};
Try using one of these standard enum values, this might help. I think 10 is irrelevant here.
Upvotes: 3
Reputation: 5667
I was struggling to get this work done & now I have it fully functional by using the default method of CollectionView.
Here it is how to scroll the collection view to the respective item index. Let's assume that I want the collection view to scroll to an item at index 3, here is the code.
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:3 inSection:0]
atScrollPosition:UICollectionViewScrollPositionNone
animated:NO];
The key factor was UICollectionViewScrollPositionNone
enum value.
Upvotes: 2