将数据从表视图控制器传递到视图控制器

时间:2017-07-15 16:00:02

标签: ios swift

我遇到从表视图控制器移动到视图控制器以显示有关所选特定单元格的信息的问题。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "SentDetailView" {
        let indexPath: NSIndexPath = self.tableView.indexPathForSelectedRow! as NSIndexPath
        let referrals = referralsSent[indexPath.row]

        let destViewController = segue.destination as! SentDetailViewController
        destViewController.referringTo = referrals.referTo
        destViewController.patientsName = referrals.patientsName
        //print("Contained value during segue " + destViewController.patientsName!)
        destViewController.patientsPhoneNumber = referrals.patientsNumber
        destViewController.patientsEmail = referrals.patientsEmail
        destViewController.comments = referrals.comment
    }
}

故事板设置:

Storyboard setup

我得到以下结果:

Result

我选择了一个单元格,它只是突出显示并且没有做任何事情。

1 个答案:

答案 0 :(得分:0)

您应该覆盖didSelectRowAt并使用发件人作为选定的索引路径调用performSegue。试试这个。

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.performSegue(withIdentifier: "SentDetailView", sender: indexPath)        
}

而且这个。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "SentDetailView"{
        let indexPath = sender as! IndexPath
        let referrals = referralsSent[indexPath.row]

        let destViewController = segue.destination as! SentDetailViewController
        destViewController.referringTo = referrals.referTo
        destViewController.patientsName = referrals.patientsName
        //print("Contained value during segue " + destViewController.patientsName!)
        destViewController.patientsPhoneNumber = referrals.patientsNumber
        destViewController.patientsEmail = referrals.patientsEmail
        destViewController.comments = referrals.comment
    }
}