选择表格单元格后,我希望它在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)
{
}
}
答案 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)
三个致命错误:
DetailViewController()
不是情节提要中的实例。DetailViewController
上执行segue。解决方案:
在didSelectRowAt
中调用self.performSegue
并将受管理对象作为sender
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
performSegue(withIdentifier: "mySegue", sender: parties[indexPath.row])
}
在DetailViewController
中声明一个NSManagedObject
属性。
class DetailViewController: UIViewController
{
var party : NSManagedObject!
...
执行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
}
在viewDidLoad
的{{1}}中,将属性分配给出口。
DetailViewController