从 tableView 单元格传递数据是点击到第二个 tableView

Pass data from tableView cell is tap to second tableView

我正在尝试在点击单元格到 detailTableView 时从 tableView 传递数据。加载详细信息 tableView 时,我没有收到任何错误。正在执行 segue,但是 tableView 保持空白。我的目标是将颜色添加到单元格中的 textLabel,并在同一单元格的 detailTextLabel 中添加 zord。谁能告诉我我做错了什么

// My data
struct PowerRangerData {
    let name: String
    let rangerColor: String
    let zord: String
}
// Intial vc

class DisplayVC: UIViewController {
    
    @IBOutlet weak var table_View: UITableView!
    
    var rangerArray = [
        PowerRangerData(name: "Jason", rangerColor: "Gold ranger", zord: "Pyramidas Zord"),
        PowerRangerData(name: "Tommy", rangerColor: "Green ranger", zord: "Dragon Zord"),
        PowerRangerData(name: "Nick", rangerColor: "Red ranger", zord: "Phoenix Zord"),
        PowerRangerData(name: "Kira", rangerColor: "Yellow ranger", zord: "Pteranodon Zord"),
        PowerRangerData(name: "Jen", rangerColor: "Pink ranger", zord: "Time flyer 5"),
        PowerRangerData(name: "Sky", rangerColor: "Blue ranger", zord: "Delta Runner 2")
    ]
    

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        table_View.dataSource = self
        table_View.delegate = self
    }
    
}

    

extension DisplayVC: UITableViewDelegate {
    
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            
            if segue.identifier == "detailSegue" {
                
                if let index_path = self.table_View.indexPathForSelectedRow {
                    
                    let detailVC = segue.destination as! DetailVC
                    
                    detailVC.color = rangerArray[index_path.row].rangerColor
                    
                    detailVC.zord = rangerArray[index_path.row].zord
                }
                
            }

        }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        table_View.deselectRow(at: indexPath, animated: true)
        
        performSegue(withIdentifier: "detailSegue", sender: self)
        
    }
    
}

extension DisplayVC: UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return rangerArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = table_View.dequeueReusableCell(withIdentifier: "cellId", for: indexPath)
        
        cell.textLabel?.text = rangerArray[indexPath.row].name
        
        return cell
    }
    
    
}
// DetailVC

class DetailVC: UIViewController {
        
    @IBOutlet weak var detailTableView: UITableView!
    
    var color = ""
    var zord = ""
    

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        detailTableView.delegate = self
        detailTableView.dataSource = self
        
    }

}

extension DetailVC: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        detailTableView.deselectRow(at: indexPath, animated: true)
    }
}

extension DetailVC: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let rangerData: [String] = [color, zord]
        return rangerData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = detailTableView.dequeueReusableCell(withIdentifier: "detailId", for: indexPath)
        
        cell.textLabel?.text = color
        cell.detailTextLabel?.text = zord
            
        return cell
    }
    
    
}


可能这里的顺序很重要 performSegue 应该在 deselectRow 之前,因为 if let index_path = self.table_View.indexPathForSelectedRow { 有一个值

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    performSegue(withIdentifier: "detailSegue", sender: self) 
    table_View.deselectRow(at: indexPath, animated: true)  
}

另外 prepare 应该在 extension DisplayVC { 下而不是 extension DisplayVC: UITableViewDelegate {