ios 如何在 IOS7 UITableView 中滚动到顶部?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19243177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to scroll to top in IOS7 UITableView?
提问by Markus Johansson
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. The table view isn't scrolled completely to the top (but almost).
在 IOS7 中,这不再起作用。表格视图没有完全滚动到顶部(但几乎)。
采纳答案by Markus Johansson
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:
通过这里其他一些答案的帮助,我设法让它工作。为了避免崩溃,我必须首先检查是否有一些部分。如果第一部分没有行,NsNotFound 可以用作行索引。希望这应该是一个放置在 UITableViewController 中的通用函数:
-(void) scrollToTop
{
if ([self numberOfSectionsInTableView:self.tableView] > 0)
{
NSIndexPath* top = [NSIndexPath indexPathForRow:NSNotFound inSection:0];
[self.tableView scrollToRowAtIndexPath:top atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
}
回答by idStar
In iOS7, whole screen UITableView and UIScrollView components, by default, adjust content and scroll indicator insets to just make everything work. However, as you've noticed CGPointZero no longer represents the content offset that takes you to the visual "top".
在 iOS7 中,全屏 UITableView 和 UIScrollView 组件,默认情况下,调整内容和滚动指示器插入以使一切正常。但是,正如您所注意到的,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.
在这里,您不必担心是否有节或行。您也没有告诉表格视图定位第一行,然后想知道为什么它没有显示所有非常高的表格标题视图等。
回答by Harikrishnan
Try this:
尝试这个:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
回答by cashmash
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 A.G
var indexPath = NSIndexPath(forRow: 0, inSection: 0)
self.sampleTableView.scrollToRowAtIndexPath(indexPath,
atScrollPosition: UITableViewScrollPosition.Top, animated: true)
or
或者
self.sampleTableView.setContentOffset(CGPoint.zero, animated:false)
回答by Jitendra
float systemVersion= [[[UIDevice currentDevice] systemVersion] floatValue];
if(systemVersion >= 7.0f)
{
self.edgesForExtendedLayout=UIRectEdgeNone;
}
Try this code in viewDidLoad()
method.
在viewDidLoad()
方法中试试这个代码。
回答by DerFlickschter
回答by RiceAndBytes
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)
回答by Lithu T.V
you can still use scrollToRowAtIndexPath:
for the purpose
您仍然可以scrollToRowAtIndexPath:
为此目的使用
回答by Chris
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. The accepted answer:
这始终保证 UITableView 将滚动到顶部。接受的答案:
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。