简体   繁体   English

从选定的 TableView 单元格 Swift Xcode 传递 var

[英]Pass var from selected TableView Cell Swift Xcode

I'm a beginner in Swift (Xcode) and I need help.我是 Swift (Xcode) 的初学者,我需要帮助。 I have a tableview and I want to send information from the indexPath.row to a segue.我有一个 tableview,我想将 indexPath.row 中的信息发送到 segue。

My script work, but the problem is the data send to my segue is the previous selected row and not the current cell select.我的脚本工作,但问题是发送到我的 segue 的数据是先前选择的行,而不是当前单元格 select。 Because the function prepare segue is called before the func tableView.因为 function prepare segue 在 func tableView 之前被调用。 That's make 2 days I search and every tutorial give me the same problem.这让我搜索了 2 天,每个教程都给我同样的问题。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    jobname = filldata[indexPath.row]
    print(jobname)
    performSegue(withIdentifier: "jobsegue", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "jobsegue" {
        let destVC = segue.destination as! JobNameViewController
        destVC.jobname = jobname
    }
}

Thank you sooo much for your help!非常感谢您的帮助!

Try this, this will work fine:试试这个,这会很好:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "jobsegue", sender: indexPath.row)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "jobsegue" {
        let destVC = segue.destination as! JobNameViewController
        destVC.jobname = filldata[sender]
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM