【问题标题】:Swift 3 - Scroll to top of Tableview on Tabbar SwitchSwift 3 - 在 Tabbar Switch 上滚动到 Tableview 的顶部
【发布时间】:2018-03-08 05:31:08
【问题描述】:

我有几个UITableViewController's 设置为UINavigationController's 的子视图,它们都是单个UITabBarController 的一部分。其中一个UITableViewController's 会在按下按钮时更新作为不同UITableViewController 一部分的tableViewController,然后通过以下方式切换当前选定的选项卡:

self.tabBarController?.selectedIndex = 1

我希望新选择的索引中的 tableView,在这种情况下为 1,在 tabViews 之间切换时自动滚动回顶部。我尝试简单地添加:

self.tableView.setContentOffset(CGPoint.zero, animated: true)

viewWillAppear(),但这似乎不起作用。如果您滚动到索引 1 上的 tableView 底部,然后切换到更新该索引(例如索引 0)的 tabBarController,更新索引并切换回索引 1,索引 1 的表视图保持在同一行就像您最初查看它时一样。数据已正确加载,但实际 UI 仍在您离开的那一行。我想实现它,以便在选项卡之间切换自动滚动到顶部。

【问题讨论】:

标签: swift scroll tableview


【解决方案1】:

您可以尝试使用 IndexPath 并滚动到 0 部分的索引 0

let indexPath = IndexPath(row: 0, section: sectionIndex) // set row = 0 and section = 0
self.tableView.scrollToRow(at: indexPath, at: .top, animated: false)

【讨论】:

    【解决方案2】:

    您可以使用scrollToRow(at:at:animated:)

    您可以将此添加到viewWillAppear(_ animated: Bool) 函数中:

    let indexPath = IndexPath(item: 0, section: 0)
    tableView.scrollToRow(at: indexPath, at: .top, animated: false)
    

    【讨论】:

    • 这不会显示largeTitlenavigationBar
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    相关资源
    最近更新 更多