ios UITableView - 滚动到顶部
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/724892/
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
UITableView - scroll to the top
提问by Ilya Suzdalnitski
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.
在我的表格视图中,我必须滚动到顶部。但我不能保证第一个对象将是第 0 行,第 0 行。可能是我的表视图将从第 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?
还有另一种滚动到表格视图顶部的方法吗?
回答by catlan
UITableView is a subclass of UIScrollView, so you can also use:
UITableView 是 UIScrollView 的子类,所以你也可以使用:
[mainTableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
Or
或者
[mainTableView setContentOffset:CGPointZero animated:YES];
And in Swift:
在 Swift 中:
mainTableView.setContentOffset(CGPointZero, animated:true)
And in Swift 3 & above:
在 Swift 3 及以上版本中:
mainTableView.setContentOffset(.zero, animated: true)
回答by fabb
Note: This answer isn't valid for iOS 11 and later.
注意:此答案对 iOS 11 及更高版本无效。
I prefer
我更喜欢
[mainTableView setContentOffset:CGPointZero animated:YES];
If you have a top inset on your table view, you have to subtract it:
如果您的表格视图中有顶部插入,则必须减去它:
[mainTableView setContentOffset:CGPointMake(0.0f, -mainTableView.contentInset.top) animated:YES];
回答by A.G
Possible Actions:
可能的操作:
1
1
func scrollToFirstRow() {
let indexPath = NSIndexPath(forRow: 0, inSection: 0)
self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true)
}
2
2
func scrollToLastRow() {
let indexPath = NSIndexPath(forRow: objects.count - 1, inSection: 0)
self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Bottom, animated: true)
}
3
3
func scrollToSelectedRow() {
let selectedRows = self.tableView.indexPathsForSelectedRows
if let selectedRow = selectedRows?[0] as? NSIndexPath {
self.tableView.scrollToRowAtIndexPath(selectedRow, atScrollPosition: .Middle, animated: true)
}
}
4
4
func scrollToHeader() {
self.tableView.scrollRectToVisible(CGRect(x: 0, y: 0, width: 1, height: 1), animated: true)
}
5
5
func scrollToTop(){
self.tableView.setContentOffset(CGPointMake(0, UIApplication.sharedApplication().statusBarFrame.height ), animated: true)
}
Disable Scroll To Top:
禁用滚动到顶部:
func disableScrollsToTopPropertyOnAllSubviewsOf(view: UIView) {
for subview in view.subviews {
if let scrollView = subview as? UIScrollView {
(scrollView as UIScrollView).scrollsToTop = false
}
self.disableScrollsToTopPropertyOnAllSubviewsOf(subview as UIView)
}
}
Modify and use it as per requirement.
根据需要修改和使用它。
Swift 4
斯威夫特 4
func scrollToFirstRow() {
let indexPath = IndexPath(row: 0, section: 0)
self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
}
回答by Kof
It's better to not use NSIndexPath (empty table), nor assume that top point is CGPointZero (content insets), that's what I use -
最好不要使用 NSIndexPath(空表),也不要假设最高点是 CGPointZero(内容插入),这就是我使用的 -
[tableView setContentOffset:CGPointMake(0.0f, -tableView.contentInset.top) animated:YES];
Hope this helps.
希望这可以帮助。
回答by Alessandro Ornano
Swift 4:
斯威夫特 4:
This works very well:
这非常有效:
//self.tableView.reloadData() if you want to use this line remember to put it before
let indexPath = IndexPath(row: 0, section: 0)
self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
回答by Adrian
I've encountered an issue calling trying some of the methods on an empty tableView. Here's another option for Swift 4 that handles empty tableviews.
我遇到了一个问题,在一个空的tableView. 这是 Swift 4 的另一个处理空表视图的选项。
extension UITableView {
func hasRowAtIndexPath(indexPath: IndexPath) -> Bool {
return indexPath.section < self.numberOfSections && indexPath.row < self.numberOfRows(inSection: indexPath.section)
}
func scrollToTop(animated: Bool) {
let indexPath = IndexPath(row: 0, section: 0)
if self.hasRowAtIndexPath(indexPath: indexPath) {
self.scrollToRow(at: indexPath, at: .top, animated: animated)
}
}
}
Usage:
用法:
// from yourViewController or yourTableViewController
tableView.scrollToTop(animated: true)//or false
回答by ScottyBlades
DONT USE
不要使用
tableView.setContentOffset(.zero, animated: true)
It can sometimes set the offset improperly. For example, in my case, the cell was actually slightly above the view with safe area insets. Not good.
它有时会不正确地设置偏移量。例如,在我的例子中,单元格实际上略高于带有安全区域插图的视图。不好。
INSTEAD USE
代替使用
tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
回答by Thanh Pham
On iOS 11, use adjustedContentInsetto correctly scroll to top for both cases when the in-call status bar is visible or not.
在 iOS 11 上,adjustedContentInset当通话中状态栏可见或不可见时,使用正确滚动到顶部的两种情况。
if (@available(iOS 11.0, *)) {
[tableView setContentOffset:CGPointMake(0, -tableView.adjustedContentInset.top) animated:YES];
} else {
[tableView setContentOffset:CGPointMake(0, -tableView.contentInset.top) animated:YES];
}
Swift:
迅速:
if #available(iOS 11.0, *) {
tableView.setContentOffset(CGPoint(x: 0, y: -tableView.adjustedContentInset.top), animated: true)
} else {
tableView.setContentOffset(CGPoint(x: 0, y: -tableView.contentInset.top), animated: true)
}
回答by Eran Goldin
For tables that have a contentInset, setting the content offset to CGPointZerowill not work. It'll scroll to the content top vs. scrolling to the table top.
对于具有 的表contentInset,将内容偏移设置为CGPointZero不起作用。它将滚动到内容顶部与滚动到桌面。
Taking content inset into account produces this instead:
考虑到内容插入会产生这个:
[tableView setContentOffset:CGPointMake(0, -tableView.contentInset.top) animated:NO];
回答by Ramesh Muthe
This code let's you scroll a specific section to top
此代码可让您将特定部分滚动到顶部
CGRect cellRect = [tableinstance rectForSection:section];
CGPoint origin = [tableinstacne convertPoint:cellRect.origin
fromView:<tableistance>];
[tableinstance setContentOffset:CGPointMake(0, origin.y)];

