我正在使用 UICollectionView,我需要在单击按钮后滚动到第一个单元格。
该按钮位于我的收藏 View 中的(大)标题部分...单击它时,我需要滚动到位于该按钮下方的第一个单元格。
我创建了这个按钮的 Action ,但我不知道如何滚动到第一个 UICollectionViewCell。
有人可以帮助我吗?
最佳答案
使用此委托(delegate)方法,将 indexPath 设置在第一个位置:
swift
self.collectionView?.scrollToItemAtIndexPath(NSIndexPath(forItem: 0, inSection: 0),
atScrollPosition: .Top,
animated: true)
swift 3
self.collectionView?.scrollToItem(at: IndexPath(row: 0, section: 0),
at: .top,
animated: true)
objective-C
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]
atScrollPosition:UICollectionViewScrollPositionTop
animated:YES];
如果您想滚动并停在不同的位置,请更改 UICollectionViewScrollPositionTop
关于ios - UICollectionView - 滚动到第一个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32262566/