2017-04-13 6 views
1

タブバーアイテムを選択するときにテーブルビューを一番上にスクロールする必要があります。 私はこれを試しましたが、うまくいきません。タブバーをタップしたときにテーブルビューを上にスクロール

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は上にスクロールされません。

答えて

5

新しいMyViewControllerインスタンスを作成して、画面にアクセスするのではなく、問題です。既に作成されたviewControllerにアクセスする必要があります。そして、このデリゲートメソッドはあなたにそれを渡します。

let myViewController = viewController as? MyViewController 
myViewController.tableView.scrollToRow(at: indexPath as IndexPath, at: .top, animated: true) 
-1
self.tableviewObject.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true) 

変更このライン

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

関連する問題