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

查看:161
本文介绍了如何在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天全站免登陆