2017-04-13 65 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,幸運的是這個委託方法把它交給你。

改變這一行

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
self.tableviewObject.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true) 
相關問題