【问题标题】:Assign UITableView content to prepare segue in Swift iOS分配 UITableView 内容以在 Swift iOS 中准备 segue
【发布时间】:2020-10-06 07:34:19
【问题描述】:

我在尝试构建 tableView 并使用 segue 准备下一个 UIViewController 屏幕时遇到问题。

所以,从屏幕 A 到屏幕 B,在屏幕 B 上,我需要显示将由屏幕 A 上按下的数据用户确定的信息。

这是屏幕 A 中 ViewController 中的代码:

extension VehicleListToInsureViewController: UITableViewDelegate {
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //print(vehicleList[indexPath.row].vehicleName)
        performSegue(withIdentifier: Constants.segueVehicleListToPolicyList, sender: self)
    
        tableView.deselectRow(at: indexPath, animated: true)
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        let destinationVC = segue.destination as! PolicyOptionsViewController
        destinationVC.vehicleName = vehicleList[indexPath.row].vehicleName
        
    }
    
}

我使用扩展名来使用UITableViewDelegate,但是这一行:

destinationVC.vehicleName = vehicleList[indexPath.row].vehicleName

抛出错误:

indexPath is unresolved identifier...

我该如何解决?我知道这是两个功能,所以我不能使用indexPath,但是如何解决这个问题以从vehicleList获取数据?

【问题讨论】:

    标签: ios swift uitableview delegates segue


    【解决方案1】:

    而不是连接 controller 的 segue,而是将其从 table view cell 连接到目标控制器。

    好处是可以删除

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //print(vehicleList[indexPath.row].vehicleName)
        performSegue(withIdentifier: Constants.segueVehicleListToPolicyList, sender: self)
    
        tableView.deselectRow(at: indexPath, animated: true)
    }
    
    罢工>

    prepare(for 被调用时,sender 参数包含cell 并且您可以向表格视图询问其索引路径

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        guard segue.identifier == Constants.segueVehicleListToPolicyList,
              let cell = sender as? UITableViewCell,
              let indexPath = tableView.indexPath(for: cell) else { return }
        
        let destinationVC = segue.destination as! PolicyOptionsViewController
        destinationVC.vehicleName = vehicleList[indexPath.row].vehicleName
        
    }
    

    【讨论】:

    • 您好 Vadian,感谢您的回复。如果我删除了 didSelectRowAt,那么我们就没有 performSegue 函数。当我单击单元格时,我不会进入下一个屏幕......所以我认为我仍然需要保留 performSegue。您提供的为 segue 准备的函数没有错误...但是 vehicleName 没有得到正确的值,它是 nil...
    • 请重读我回答的第一句话。您必须在 Interface Builder 中重新连接 segue。
    • 是的,我的错!谢谢老哥!
    【解决方案2】:

    您可以通过sender 参数将索引路径(或其他任何您想要的)传递给prepareForSegue

    performSegue(withIdentifier: Constants.segueVehicleListToPolicyList, 
                 sender: indexPath) // <---- !!
    

    prepareForSegue,你可以尝试将sender转换成IndexPath

    if let destinationVC = segue.destination as? PolicyOptionsViewController,
       let indexPath = sender as? IndexPath {
       destinationVC.vehicleName = vehicleList[indexPath.row].vehicleName
    }
    

    或者,直接将vehicleList[indexPath.row].vehicleName作为发件人传递,并转换为String

    performSegue(withIdentifier: Constants.segueVehicleListToPolicyList, 
                 sender: vehicleList[indexPath.row].vehicleName)
    // ...
    if let destinationVC = segue.destination as? PolicyOptionsViewController,
       let vehicleName = sender as? String {
       destinationVC.vehicleName = vehicleName
    }
    

    【讨论】:

    • 感谢 Sweeper 的回复,使用您的第一个解决方案,我得到了条件绑定的初始化程序必须具有 Optional 类型,而不是 if let destinationVC = ..... 行的“PolicyOptionsViewController”错误,如果我修改,则第二个解决方案如您所建议的 performSegue 功能,我应该如何处理准备 Segue 功能?如果我按照您的建议修改 performSegue 函数,则会出现太多错误......
    • @Yudi 哎呀!有一些错别字。将!? 替换为?
    • 现在工作,欣赏!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多