点击标签栏时将桌面视图滚动到顶部

时间:2017-04-13 21:07:03

标签: ios swift uitableview tableview

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

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不会滚动到顶部。

2 个答案:

答案 0 :(得分:5)

问题是您正在创建新的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)

答案 1 :(得分:-1)

self.tableviewObject.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true)
相关问题