简体   繁体   English

UICollectionView滚动到特定部分

[英]UICollectionView scroll to specific section

Using Objective C and Xcode I have created a UICollectionView with 10 sections and all works fine. 使用Objective C和Xcode我创建了一个包含10个部分的UICollectionView ,一切正常。 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. 默认情况下它显示0-9部分,但我希望它从第5部分开始。

You can call scrollToItemAtIndexPath:atScrollPosition:animated: in your viewDidAppear: method. 您可以在viewDidAppear:方法中调用scrollToItemAtIndexPath:atScrollPosition:animated: .

You can hold a reference to the cell you want to scroll to, then use indexPathForCell: to get the index path. 您可以保存对要滚动到的单元格的引用,然后使用indexPathForCell:来获取索引路径。

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:. 创建NSIndexPath的方法,而不是保持对单元格的引用,然后调用indexPathForCell :. Also, you can move this code to the viewWillAppear: method if you want the scrolling to happen before the view is displayed. 此外,如果要在显示视图之前进行滚动,可以将此代码移动到viewWillAppear:方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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