gpt4 book ai didi

swift - 如何让单元格在我的 ViewController2 中打印?

转载 作者:可可西里 更新时间:2023-10-31 23:44:23 26 4
gpt4 key购买 nike

我正在尝试让我的应用程序在不同的屏幕上显示所选单元格的名称。我让它显示“样本”一词,但我想显示所选人员的姓名。我能做些什么来显示它?这只是我需要在 prepareforSegue 中更改的一行代码。

View Controller :

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")
}

}

最佳答案

这很容易完成,而不是在执行 performSegueWithIdentifier("selectedView", sender: tableView) 时发送 tableView 作为发送方发送所选行的 indexPath,因此你有:

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
}
}
}

关于swift - 如何让单元格在我的 ViewController2 中打印?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34148086/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com