ios - 点击标签栏时将表格 View 滚动到顶部

标签 ios swift uitableview tableview

选择标签栏项目时,我需要一直向上滚动表格 View 。 我试过了,但它不起作用。

func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
    let tabBarIndex = tabBarController.selectedIndex
    if tabBarIndex == 0 {

        let indexPath = NSIndexPath(row: 0, section: 0)
        MyViewController().tableView.scrollToRow(at: indexPath as IndexPath, at: .top, animated: true)
    }
}

调用了该方法,但 tableView 没有滚动到顶部。

最佳答案

问题是您正在创建一个新的 MyViewController 实例,而不是访问屏幕上的实例。您需要访问已经创建的 viewController,幸运的是 this delegate 方法将它交给了您。

改变这一行

MyViewController().tableView.scrollToRow(at: indexPath as IndexPath, at: .top, animated: true)

let myViewController = viewController as? MyViewController
myViewController.tableView.scrollToRow(at: indexPath as IndexPath, at: .top, animated: true)

关于ios - 点击标签栏时将表格 View 滚动到顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43401727/

相关文章:

ios - UITableView - 如何识别我是否将单元格拖到单元格上?

iOS UIAutomation 失败 :The target application appears to have died

ios - 如何在 inputView 中设置默认日期值

ios - 在详细信息标题栏中,显示 “Picture X of Y”

ios - Swift 4 Refresh Control不流畅但总是跳

ios - 快速检查网络速度

ios - 在以下情况下应该使用哪个 In App Purchase?

ios - 实例化类不起作用 - swift

ios - OpenGL-ES:选择性混合

ios - swift : "Mixing in"属性