UITableView - 滚动到顶部 [英] UITableView - scroll to the top

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

问题描述

在我的表格视图中,我必须滚动到顶部.但我不能保证第一个对象将是第 0 行,第 0 行.可能是我的表视图将从第 5 节开始.

In my table view I have to scroll to the top. But I cannot guarantee that the first object is going to be section 0, row 0. May be that my table view will start from section number 5.

所以当我调用时,我得到一个异常:

So I get an exception, when I call:

[mainTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];

还有其他方法可以滚动到表格视图的顶部吗?

Is there another way to scroll to the top of table view?

推荐答案

UITableView 是 UIScrollView 的子类,所以你也可以使用:

UITableView is a subclass of UIScrollView, so you can also use:

[mainTableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];

[mainTableView setContentOffset:CGPointZero animated:YES];

在 Swift 中:

mainTableView.setContentOffset(CGPointZero, animated:true)

在 Swift 3 &上图:

And in Swift 3 & above:

mainTableView.setContentOffset(.zero, animated: true)

这篇关于UITableView - 滚动到顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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