滚动到Table View Header顶部

SRM*_*RMR 4 uitableview ios swift

我有一个UITableView标题占据屏幕的整个帧.

每次应用程序进入后台,然后返回前台/活动状态时,我希望将屏幕设置回标题顶部.

我只看到了与滚动到表格顶部的答案,这些答案使用类似的东西self.tableView.scrollToRow(at: top, at: .top, animated: true),但是这不起作用,因为它只会滚动到第0部分的第0行而UITableView不是顶部的头的UITableView.

有任何想法吗?

谢谢!

编辑:试过这个

override func viewWillAppear(_ animated: Bool) {
    self.tableView.setContentOffset( CGPoint(x: 0, y: 0) , animated: true)
}
Run Code Online (Sandbox Code Playgroud)

这里有一些值得注意的表设置代码:

// Set up Frames
let headerFrame: CGRect = UIScreen.main.bounds
// Background Table
self.tableView.isPagingEnabled = true

// Set up Table Header
let header = UIView(frame: headerFrame)
self.tableView.tableHeaderView = header    
Run Code Online (Sandbox Code Playgroud)

Ily*_*pan 11

TableView是一个滚动视图子类,所以我想这应该工作:

 self.tableView.setContentOffset( CGPoint(x: 0, y: 0) , animated: true)
Run Code Online (Sandbox Code Playgroud)

当应用程序进入前台时,不会调用ViewWillAppear.注册VC为UIApplicationWillEnterForegroundNotification


Moh*_*mar 8

您可以滚动到节标题,尝试以下代码:

DispatchQueue.main.async {
    let indexPath = IndexPath(row: NSNotFound, section: yourSectionIndex)
    self.tableView.scrollToRow(at: indexPath, at: .bottom, animated: true)
}
Run Code Online (Sandbox Code Playgroud)

对于行添加:NSNotFound

根据您的要求更改“scrollToRow”中的.top/.bottom。