【问题标题】:Scroll tableview to the top when tapping tab bar点击标签栏时将表格视图滚动到顶部
【发布时间】:2017-04-13 21:07:02
【问题描述】:

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

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没有滚动到顶部。

【问题讨论】:

    标签: ios swift uitableview tableview


    【解决方案1】:

    问题是您正在创建一个新的MyViewController 实例,而不是访问屏幕上的实例。你会想要访问已经创建的 viewController,幸运的是这个委托方法把它交给了你。

    改变这一行

    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)
    

    【讨论】:

      【解决方案2】:
      self.tableviewObject.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-13
        • 2016-04-02
        • 2012-06-06
        • 1970-01-01
        相关资源
        最近更新 更多