简体   繁体   中英

UICollectionView scroll to specific section

Using Objective C and Xcode I have created a UICollectionView with 10 sections and all works fine. Is there anyway to set the section to which is viewed when first shown? By default it shows sections 0-9, but I want it to start at section 5.

You can call scrollToItemAtIndexPath:atScrollPosition:animated: in your viewDidAppear: method.

You can hold a reference to the cell you want to scroll to, then use indexPathForCell: to get the index path.

The following are the scroll positions you can choose from

UICollectionViewScrollPositionNone
UICollectionViewScrollPositionTop
UICollectionViewScrollPositionCenteredVertically
UICollectionViewScrollPositionBottom
UICollectionViewScrollPositionLeft
UICollectionViewScrollPositionCenteredHorizontally
UICollectionViewScrollPositionRight

In case you know where you want to scroll to in terms of an integer row and section, you can use those integers in the

NSIndexPath indexPathForItem:inSection:

method to create an NSIndexPath instead of keeping a reference to a cell and then calling indexPathForCell:. Also, you can move this code to the viewWillAppear: method if you want the scrolling to happen before the view is displayed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM