在表格视图中,我必须滚动到顶部。但我不能保证第一个对象是section 0,第0行。可能我的表视图将从第5节开始。
所以当我调用:
[mainTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
有其他方法滚动到表视图的顶部吗?
在表格视图中,我必须滚动到顶部。但我不能保证第一个对象是section 0,第0行。可能我的表视图将从第5节开始。
所以当我调用:
[mainTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
有其他方法滚动到表视图的顶部吗?
当前回答
在斯威夫特
your row = selectioncellRowNumber 你的section如果你有= selectionNumber如果你没有set为零
/ / UITableViewScrollPosition。中间或底部或顶部
var lastIndex = NSIndexPath(forRow: selectioncellRowNumber, inSection: selectionNumber)
self.tableView.scrollToRowAtIndexPath(lastIndex, atScrollPosition: UITableViewScrollPosition.Middle, animated: true)
其他回答
不使用
tableView.setContentOffset(.zero, animated: true)
它有时会不正确地设置偏移量。例如,在我的例子中,单元格实际上略高于带有安全区域嵌入的视图。不好的。
而不是使用
tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
如果你不想要滚动,你可以启动和停止滚动动画,只要你启动它。
$('document').ready(function() {
$("html, body").animate({ scrollTop: 0 }, 500);
return true;
});
此外,为了使'x'和'y'的值动画化,传入0,0将立即将页面滚动到左上角。
window.scrollTo(x, y);
Swift 4通过扩展,处理空表视图:
extension UITableView {
func scrollToTop(animated: Bool) {
self.setContentOffset(CGPoint.zero, animated: animated);
}
}
以下是我在iOS 11上正确运行的方法:
extension UIScrollView {
func scrollToTop(animated: Bool) {
var offset = contentOffset
if #available(iOS 11, *) {
offset.y = -adjustedContentInset.top
} else {
offset.y = -contentInset.top
}
setContentOffset(offset, animated: animated)
}
}
在Swift-3中:
self.tableView.setContentOffset(CGPoint.zero, animated: true)