选择标签栏项目时,我需要一直向上滚动表格 View 。 我试过了,但它不起作用。
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)
到
let myViewController = viewController as? MyViewController
myViewController.tableView.scrollToRow(at: indexPath as IndexPath, at: .top, animated: true)
关于ios - 点击标签栏时将表格 View 滚动到顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43401727/