gpt4 book ai didi

ios - 点击标签栏时将表格 View 滚动到顶部

转载 作者:搜寻专家 更新时间:2023-11-01 06:16:38 26 4
gpt4 key购买 nike

选择标签栏项目时,我需要一直向上滚动表格 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com