Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 轻触选项卡栏时将tableview滚动到顶部_Ios_Swift_Uitableview_Tableview - Fatal编程技术网

Ios 轻触选项卡栏时将tableview滚动到顶部

Ios 轻触选项卡栏时将tableview滚动到顶部,ios,swift,uitableview,tableview,Ios,Swift,Uitableview,Tableview,在选择选项卡栏项目时,我需要一直向上滚动表格视图。 我试过了,但没用 func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) { let tabBarIndex = tabBarController.selectedIndex if tabBarIndex == 0 { let indexPath = NSI

在选择选项卡栏项目时,我需要一直向上滚动表格视图。 我试过了,但没用

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)


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

换行

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

self.tableviewObject.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true)