[英]Scroll tableview to the top when tapping tab bar
I need to scroll table view all the way up when selecting the tab bar item. 选择选项卡栏项时,我需要一直滚动表格视图。 I tried this but it does not work. 我试过了,但是没有用。
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)
}
}
The method is called but the tableView does not scroll to the top. 该方法被调用,但是tableView不会滚动到顶部。
The problem is you're creating a new MyViewController
instance rather than accessing the one on screen. 问题是您要创建一个新的MyViewController
实例,而不是访问屏幕上的实例。 You'll want to access the viewController already created and luckily the this delegate method hands that to you. 您将要访问已经创建的viewController,幸运的是,此委托方法会将其交给您。
Change this line 更改此行
MyViewController().tableView.scrollToRow(at: indexPath as IndexPath, at: .top, animated: true)
to 至
let myViewController = viewController as? MyViewController
myViewController.tableView.scrollToRow(at: indexPath as IndexPath, at: .top, animated: true)
self.tableviewObject.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.