UICollectionView - 滚动到第一个单元格 [英] UICollectionView - Scrolling to first cell

查看:327
本文介绍了UICollectionView - 滚动到第一个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用UICollectionView,我需要在单击按钮后滚动到第一个单元格。

I'm making use of UICollectionView and I need to scroll to first cell after click in a button.

该按钮位于(大)标题部分在我的集合视图中...当点击它时,我需要滚动到我的第一个单元格位于此按钮下方。

The button is located in a (big) header section in my collection view... when clicking int it, I need scroll to my first cell located bellow of this button.

我创建了此按钮的动作,但是我不知道如何滚动到第一个UICollectionViewCell。

I created a action of this button, but I don't know how to scroll to first UICollectionViewCell.

有人能帮帮我吗?

推荐答案

使用此委托方法,将indexPath设置在第一个位置:

Use this delegate method, setting the indexPath at the first position:

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

Objective-C

[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] 
                            atScrollPosition:UICollectionViewScrollPositionTop
                                    animated:YES];

如果要滚动和更改 UICollectionViewScrollPositionTop 停在不同的位置

Change UICollectionViewScrollPositionTop if you want to scroll and stop at a different position

这篇关于UICollectionView - 滚动到第一个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆