在表格视图中,我必须滚动到顶部。但我不能保证第一个对象是section 0,第0行。可能我的表视图将从第5节开始。

所以当我调用:

[mainTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];

有其他方法滚动到表视图的顶部吗?


当前回答

使用这段代码在swift中实现UITableview:

var cell = tableView.dequeueReusableCellWithIdentifier(“cell”)
if cell == nil {
    cell = UITableViewCell(style: .Value1, reuseIdentifier: “cell”)
}

其他回答

如果你不想要滚动,你可以启动和停止滚动动画,只要你启动它。

    $('document').ready(function() {
    $("html, body").animate({ scrollTop: 0 }, 500);
    return true;
    });

此外,为了使'x'和'y'的值动画化,传入0,0将立即将页面滚动到左上角。

window.scrollTo(x, y);

对于具有contentInset的表,将内容偏移量设置为CGPointZero将不起作用。它会滚动到内容顶部,而不是滚动到桌面顶部。

将内容嵌入考虑在内会产生这样的结果:

[tableView setContentOffset:CGPointMake(0, -tableView.contentInset.top) animated:NO];

要在完成时有一个完成,请添加此扩展

// MARK: - UIScrollView

extension UIScrollView {

    /// Animate scroll to top with completion
    ///
    /// - Parameters:
    ///   - duration:   TimeInterval
    ///   - completion: Completion block
    func animateScrollToTop(withDuration duration:  TimeInterval,
                            completion:             @escaping (()->())) {

        UIView.animate(withDuration: duration, animations: { [weak self] in
                self?.setContentOffset(CGPoint.zero, animated: false)
            }, completion: { finish in
                guard finish else {
                    return
                }
                completion()
        })
    }
}

tableView.animateScrollToTop(withDuration: 0.25) { 
    // Finish
}

在斯威夫特

your row = selectioncellRowNumber 你的section如果你有= selectionNumber如果你没有set为零

/ / UITableViewScrollPosition。中间或底部或顶部

var lastIndex = NSIndexPath(forRow:  selectioncellRowNumber, inSection: selectionNumber)
self.tableView.scrollToRowAtIndexPath(lastIndex, atScrollPosition: UITableViewScrollPosition.Middle, animated: true)

迅速:

如果你没有tableView头文件:

tableView.setContentOffset(CGPointMake(0,  UIApplication.sharedApplication().statusBarFrame.height ), animated: true)

如果有:

tableView.setContentOffset(CGPointMake(0, -tableViewheader.frame.height   + UIApplication.sharedApplication().statusBarFrame.height ), animated: true)