我正在尝试让我的应用在不同的屏幕上显示所选单元格的名称。我得到它来显示" Sample"但我想显示所选人员的姓名。我能做些什么来表现呢?它只是我在prepareforSegue中需要改变的一行代码。
的ViewController:
import UIKit
import CoreData
class ViewController: UIViewController,UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var ppl = [NSManagedObject]()
@IBAction func addName(sender: AnyObject) {
let alert = UIAlertController(title: "New Client",
message: "Add a new client",
preferredStyle: .Alert)
let saveAction = UIAlertAction(title: "Save",
style: .Default,
handler: { (action:UIAlertAction) -> Void in
let textField = alert.textFields!.first
self.saveName(textField!.text!)
self.tableView.reloadData()
})
let cancelAction = UIAlertAction(title: "Cancel",
style: .Default) { (action: UIAlertAction) -> Void in
}
alert.addTextFieldWithConfigurationHandler {
(textField: UITextField) -> Void in
}
alert.addAction(saveAction)
alert.addAction(cancelAction)
presentViewController(alert,
animated: true,
completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
title = "Clients"
accessData()
tableView.registerClass(UITableViewCell.self,
forCellReuseIdentifier: "Cell")
tableView.reloadData()
}
func tableView(tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return ppl.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell =
tableView.dequeueReusableCellWithIdentifier("Cell")
let person = ppl[indexPath.row]
cell!.textLabel!.text =
person.valueForKey("name") as? String
return cell!
}
func tableView(tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("SELECTED ITEM")
performSegueWithIdentifier("selectedView", sender: tableView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func saveName(name: String) {
let appDelegate =
UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let entity = NSEntityDescription.entityForName("Person",
inManagedObjectContext:managedContext)
let person = NSManagedObject(entity: entity!,
insertIntoManagedObjectContext: managedContext)
person.setValue(name, forKey: "name")
do {
try managedContext.save()
ppl.append(person)
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}
}
func accessData() {
let appDelegate =
UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let fetchRequest = NSFetchRequest(entityName: "Person")
do {
let results =
try managedContext.executeFetchRequest(fetchRequest)
ppl = results as! [NSManagedObject]
} catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier=="selectedView") {
let dvc=segue.destinationViewController as! ViewController2
dvc.name="Sample"
//let selectedItem: NSManagedObject = ppl[self.tableView.indexPathForSelectedRow!.row] as NSManagedObject
//dvc.name = selectedItem.valueForKey("Cell") as! String
}
}
}
ViewController2:
import UIKit
class ViewController2:UIViewController{
@IBOutlet weak var userDisplay: UILabel!
var name = String()
override func viewDidLoad() {
super.viewDidLoad()
userDisplay.text = name
}
func printMe(){
print("I am a controller")
}
}
答案 0 :(得分:0)
这可以轻松完成,而不是在performSegueWithIdentifier("selectedView", sender: tableView)
发送所选行的indexPath
时发送tableView作为发件人,因此您拥有:
performSegueWithIdentifier("selectedView", sender: indexPath)
然后你需要做的就是将prepareForSegue
方法改为
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let indexPath = sender {
let cell = self.tableView.cellForRowAtIndexPath(indexPath as! NSIndexPath)
if let name = cell?.textLabel?.text {
let dvc = segue.destinationViewController as! SecondVC
dvc.name = name
}
}
}