如何在 IOS7 UITableView 中滚动到顶部? [英] How to scroll to top in IOS7 UITableView?

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

问题描述

在 IOS6 中,我有以下代码滚动到 UITableView 的顶部

In IOS6 I have the following code to scroll to the top of a UITableView

[tableView setContentOffset:CGPointZero animated:YES];

在 IOS7 中,这不再起作用.表格视图没有完全滚动到顶部(但几乎没有).

In IOS7 this doesn't work anymore. The table view isn't scrolled completely to the top (but almost).

推荐答案

通过这里其他一些答案的帮助,我设法让它正常工作.为了避免崩溃,我必须首先检查是否有一些部分.如果第一部分没有行,NsNotFound 可以用作行索引.希望这应该是放置在 UITableViewController 中的通用函数:

By the help from some other answers here I managed to get it working. To avoid a crash I must first check that there are some sections. NsNotFound can be used as a row index if the first section has no rows. Hopefully this should be a generic function to be placed in a UITableViewController:

-(void) scrollToTop
{
    if ([self numberOfSectionsInTableView:self.tableView] > 0)
    {
        NSIndexPath* top = [NSIndexPath indexPathForRow:NSNotFound inSection:0];
        [self.tableView scrollToRowAtIndexPath:top atScrollPosition:UITableViewScrollPositionTop animated:YES];
    }
}

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

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