在表格视图中,我必须滚动到顶部。但我不能保证第一个对象是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];
有其他方法滚动到表视图的顶部吗?
当前回答
迅速:
如果你没有tableView头文件:
tableView.setContentOffset(CGPointMake(0, UIApplication.sharedApplication().statusBarFrame.height ), animated: true)
如果有:
tableView.setContentOffset(CGPointMake(0, -tableViewheader.frame.height + UIApplication.sharedApplication().statusBarFrame.height ), animated: true)
其他回答
这是一个简单的UIScrollView扩展的例子,你可以在你的应用程序的任何地方使用。
1)首先你应该创建一个包含滚动方向的枚举:
enum ScrollDirection {
case top, right, bottom, left
func contentOffsetWith(_ scrollView: UIScrollView) -> CGPoint {
var contentOffset = CGPoint.zero
switch self {
case .top:
contentOffset = CGPoint(x: 0, y: -scrollView.contentInset.top)
case .right:
contentOffset = CGPoint(x: scrollView.contentSize.width - scrollView.bounds.size.width, y: 0)
case .bottom:
contentOffset = CGPoint(x: 0, y: scrollView.contentSize.height - scrollView.bounds.size.height)
case .left:
contentOffset = CGPoint(x: -scrollView.contentInset.left, y: 0)
}
return contentOffset
}
}
2)然后添加扩展到UIScrollView:
extension UIScrollView {
func scrollTo(direction: ScrollDirection, animated: Bool = true) {
self.setContentOffset(direction.contentOffsetWith(self), animated: animated)
}
}
3)就是这样!现在你可以使用它:
myScrollView.scrollTo(.top, animated: false)
这个滚动绑定到tableView的内容大小,它看起来比滚动到CGPoint.zero更自然
我更喜欢下面的,因为它考虑到一个插图。如果没有插入,它仍然会滚动到顶部,因为插入将是0。
tableView.setContentOffset(CGPoint(x: 0, y: -tableView.contentInset.top), animated: true)
在Swift-3中:
self.tableView.setContentOffset(CGPoint.zero, animated: true)
func scrollToTop() {
NSIndexPath *topItem = [NSIndexPath indexPathForItem:0 inSection:0];
[tableView scrollToRowAtIndexPath:topItem atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
调用这个函数,无论你想让UITableView滚动到顶部
可能的行动:
1
func scrollToFirstRow() {
let indexPath = NSIndexPath(forRow: 0, inSection: 0)
self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true)
}
2
func scrollToLastRow() {
let indexPath = NSIndexPath(forRow: objects.count - 1, inSection: 0)
self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Bottom, animated: true)
}
3
func scrollToSelectedRow() {
let selectedRows = self.tableView.indexPathsForSelectedRows
if let selectedRow = selectedRows?[0] as? NSIndexPath {
self.tableView.scrollToRowAtIndexPath(selectedRow, atScrollPosition: .Middle, animated: true)
}
}
4
func scrollToHeader() {
self.tableView.scrollRectToVisible(CGRect(x: 0, y: 0, width: 1, height: 1), animated: true)
}
5
func scrollToTop(){
self.tableView.setContentOffset(CGPointMake(0, UIApplication.sharedApplication().statusBarFrame.height ), animated: true)
}
禁用滚动到顶部:
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)
}
}
根据需求修改和使用它。
斯威夫特4
func scrollToFirstRow() {
let indexPath = IndexPath(row: 0, section: 0)
self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
}