所选表格单元格信息未使用核心数据显示在详细视图中,没有错误

时间:2019-04-26 15:44:22

标签: swift uitableview core-data

选择表格单元格后,我希望它在segue之后在我的详细信息视图中填充文本字段。

这是我在此网站上找到的一种方法(我尝试了其他在这里看到的方法,但是在将代码转换为与核心数据和/或自定义单元格结合使用时出现错误/问题),但不会返回错误,填写字段


    func tableView(_ tableView: UITableView,
                   cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        //(print(tableView.dequeueReusableCell(withIdentifier: "PartyCell", for: indexPath)))

        //  get selected row (party)
        let party = parties[indexPath.row] as NSManagedObject

        //  create custom cell
        let cell = tableView.dequeueReusableCell(withIdentifier: "PartyCell",
                                                 for: indexPath) as! PartyCell

        //  Update the custom cell labels with information from record
        cell.nameLabel?.text = party.value(forKeyPath: "name") as? String
        cell.sizeLabel.text = party.value(forKeyPath: "size") as? String
        cell.contactLabel.text = party.value(forKeyPath: "contact") as? String
        cell.locationLabel.text = party.value(forKeyPath: "location") as? String

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {


        // Create a variable that you want to send based on the destination view controller
        // Get a reference to the data by using indexPath shown below
        let party = parties[indexPath.row]

        // Create an instance of DestinationViewController and pass the variable
        let destinationVC = DetailViewController()


        destinationVC.nameField.text = party.value(forKeyPath: "name") as? String
        destinationVC.sizeField.text = party.value(forKeyPath: "size") as? String
        destinationVC.contactField.text = party.value(forKeyPath: "contact") as? String
        destinationVC.locationField.text = party.value(forKeyPath: "name") as? String


        // Let's assume that the segue name is called playerSegue
        // This will perform the segue and pre-load the variable for you to use
        destinationVC.performSegue(withIdentifier: "mySegue", sender: self)

    }

这是已建立插座连接的详细视图

import Foundation
import UIKit
import CoreData

class DetailViewController: UIViewController
{

    //let party = NSEntityDescription.insertNewObjectForEntityForName("Party", inManagedObjectContext: managedObjectContext) as! Party


    @IBOutlet weak var nameField: UITextField!
    @IBOutlet weak var sizeField: UITextField!
    @IBOutlet weak var contactField: UITextField!
    @IBOutlet weak var locationField: UITextField!

    override func viewWillAppear(_ animated: Bool)
    {

    }

}

2 个答案:

答案 0 :(得分:0)

您可以在您的detailviewcontroller中添加一个变量:

class DetailViewController: UIViewController { 
    var party: Party!
}

使用segue传递所选方;所以在didSelectRowAt

let destinationVC = UIStoryboard(name: "yourStory", bundle: nil).instantiateViewController(withIdentifier: "yourId") as! DetailViewController()
destinationVC.party = selectedParty
destinationVC.performSegue(withIdentifier: "mySegue", sender: self)

然后在您的viewDidLoad中的DetailViewController中设置营业网点:

override func viewDidLoad() {
   super.viewDidLoad()
   nameField.text = party.something
//and so on!
}

答案 1 :(得分:0)

三个致命错误:

  1. DetailViewController()不是情节提要中的实例。
  2. 即使在初始化控制器后,插座仍未连接。
  3. 您必须在主视图控制器上而不是在DetailViewController上执行segue。

解决方案:

  1. didSelectRowAt中调用self.performSegue并将受管理对象作为sender

    传递
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
        performSegue(withIdentifier: "mySegue", sender: parties[indexPath.row])
    }
    
  2. DetailViewController中声明一个NSManagedObject属性。

    class DetailViewController: UIViewController
    {
    
        var party : NSManagedObject!
    
        ...
    
  3. 执行prepare(for segue并将NSManagedObject对象分配给临时属性。

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        guard segue.identifier == "mySegue" else { return }
        let detailViewController = segue.destination as! DetailViewController
        detailViewController.party = sender as! NSManagedObject
    }
    
  4. viewDidLoad的{​​{1}}中,将属性分配给出口。

    DetailViewController