iOS UICollectionView 添加 headerView 分组后滚动到指定的section

Posted 夏天然后

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS UICollectionView 添加 headerView 分组后滚动到指定的section相关的知识,希望对你有一定的参考价值。

方案 1

UICollectionViewLayoutAttributes *attributes = [self.collectionView layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:DJSection]];
                CGRect rect = attributes.frame;
                [self.collectionView setContentOffset:CGPointMake(self.collectionView.frame.origin.x, rect.origin.y - DJ_ADAPT_750(120)) animated:NO];

方案 2

[_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:index] atScrollPosition:UICollectionViewScrollPositionTop animated:YES];

方案有个缺点, headerView 会遮挡当前 cell, 所以推荐方案 1

以上是关于iOS UICollectionView 添加 headerView 分组后滚动到指定的section的主要内容,如果未能解决你的问题,请参考以下文章