简体   繁体   English

如何在IOS7 UITableView中滚动到顶部?

[英]How to scroll to top in IOS7 UITableView?

In IOS6 I have the following code to scroll to the top of a UITableView 在IOS6中,我有以下代码滚动到UITableView的顶部

[tableView setContentOffset:CGPointZero animated:YES];

In IOS7 this doesn't work anymore. 在IOS7中,这不再起作用了。 The table view isn't scrolled completely to the top (but almost). 表视图不会完全滚动到顶部(但几乎)。

In iOS7, whole screen UITableView and UIScrollView components, by default, adjust content and scroll indicator insets to just make everything work. 在iOS7中,默认情况下,整个屏幕UITableView和UIScrollView组件调整内容并滚动指示符插入,以使一切正常。 However, as you've noticed CGPointZero no longer represents the content offset that takes you to the visual "top". 但是,正如您已经注意到CGPointZero不再代表将您带到视觉“顶部”的内容偏移。

Use this instead: 请改用:

self.tableView.contentOffset = CGPointMake(0, 0 - self.tableView.contentInset.top);

Here, you don't have to worry about if you have sections or rows. 在这里,您不必担心是否有部分或行。 You also don't tell the Table View to target the first row, and then wonder why it didn't show all of your very tall table header view, etc. 您也不会告诉Table View定位第一行,然后想知道为什么它没有显示所有非常高的表头视图等。

Try this: 试试这个:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

Based on the accepted answer from @Markus Johansson, here is the Swift code version: 基于@Markus Johansson接受的答案,这里是Swift代码版本:

func scrollToTop() {
    if (self.tableView.numberOfSections > 0 ) {
        let top = NSIndexPath(row: Foundation.NSNotFound, section: 0)
        self.tableView.scrollToRow(at: top as IndexPath, at: .top, animated: true);
    }
}

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. 如果第一部分没有行,则NsNotFound可用作行索引。 Hopefully this should be a generic function to be placed in a UITableViewController: 希望这应该是放在UITableViewController中的通用函数:

-(void) scrollToTop
{
    if ([self numberOfSectionsInTableView:self.tableView] > 0)
    {
        NSIndexPath* top = [NSIndexPath indexPathForRow:NSNotFound inSection:0];
        [self.tableView scrollToRowAtIndexPath:top atScrollPosition:UITableViewScrollPositionTop animated:YES];
    }
}
var indexPath = NSIndexPath(forRow: 0, inSection: 0)
self.sampleTableView.scrollToRowAtIndexPath(indexPath,
atScrollPosition: UITableViewScrollPosition.Top, animated: true)

or 要么

self.sampleTableView.setContentOffset(CGPoint.zero, animated:false)
float systemVersion= [[[UIDevice currentDevice] systemVersion] floatValue];

if(systemVersion >= 7.0f)
{
  self.edgesForExtendedLayout=UIRectEdgeNone;   
}

Try this code in viewDidLoad() method. viewDidLoad()方法中尝试此代码。

Here is idStar's answer in updated Swift 3 syntax: 这是idStar在更新的Swift 3语法中的答案

self.tableView.contentOffset = CGPoint(x: 0, y: 0 - self.tableView.contentInset.top)

With animation: 随着动画:

self.tableView.setContentOffset(CGPoint(x: 0, y: 0 - self.tableView.contentInset.top), animated: true)

Swift 3 斯威夫特3

If you have table view headers CGPointZero may not work for you, but this always does the trick to scroll to top. 如果你有表视图标题CGPointZero可能不适合你,但这总是可以滚动到顶部。

self.tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: UITableViewScrollPosition.top, animated: false)

你仍然可以使用scrollToRowAtIndexPath:为此目的

I realize this has been answered but I just wanted to give another option: 我意识到这已经得到了解答,但我只想提出另一种选择:

CGRect frame = {{0, 0},{1, 1}};
[self.tableView scrollRectToVisible:frame animated:YES];

This always guarantees the UITableView will scroll to the top. 这始终保证UITableView将滚动到顶部。 The accepted answer: 接受的答案是:

NSIndexPath* top = [NSIndexPath indexPathForRow:NSNotFound inSection:0];
[self.tableView scrollToRowAtIndexPath:top atScrollPosition:UITableViewScrollPositionTop animated:YES];

was not working for me because I was scrolling to a tableHeaderView and not a cell. 我没有为我工作,因为我滚动到tableHeaderView而不是单元格。

Using scrollRectToVisible works on iOS 6 and 7. 使用scrollRectToVisible适用于iOS 6和7。

Swift 4 UITableViewExtension: Swift 4 UITableViewExtension:

func scrollToTop(animated: Bool) {
    if numberOfSections > 0 {
        let topIndexPath = IndexPath(row: NSNotFound, section: 0)
        scrollToRow(at: topIndexPath, at: .top, animated: animated)
    }
}

in swift I used: 在swift我用过:

self.tableView.setContentOffset(CGPointMake(0, 0), animated: true)

but @Alvin George's works great 但@Alvin George的作品很棒

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM