CoreData与Swift

时间:2015-12-02 02:52:03

标签: swift

我是swift和核心数据的新手。 我试图显示两个文本字段,一旦您单击保存,它们将转到核心数据并放在表格视图中。我有一个文本字段可以工作,但我需要另一个工作。谁能告诉我我做错了什么..

我已经将两个属性添加到核心数据中,一个是“名称”,另一个是“描述”。

以下是我在视图控制器中的内容..

谢谢!

类ViewController:UIViewController,UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!

var people = [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!)

            let textF = alert.textFields!.first
            self.saveName(textF!.text!)
            self.tableView.reloadData()
    })

    let cancelAction = UIAlertAction(title: "Cancel",
        style: .Default) { (action: UIAlertAction) -> Void in
    }

    alert.addTextFieldWithConfigurationHandler {
        (textField: UITextField) -> Void in

        (textF: UITextField) -> Void in
    }


    alert.addAction(saveAction)
    alert.addAction(cancelAction)

    presentViewController(alert,
        animated: true,
        completion: nil)
}


override func viewDidLoad() {
    super.viewDidLoad()
    title = "\"Clients\""
    tableView.registerClass(UITableViewCell.self,
        forCellReuseIdentifier: "Cell")

}

func tableView(tableView: UITableView,
    numberOfRowsInSection section: Int) -> Int {
        return people.count
}

func tableView(tableView: UITableView,
    cellForRowAtIndexPath
    indexPath: NSIndexPath) -> UITableViewCell {

        let cell =
        tableView.dequeueReusableCellWithIdentifier("Cell")

        let person = people[indexPath.row]

        cell!.textLabel!.text =
            person.valueForKey("name") as? String
            person.valueForKey("description") as? String

        return cell!
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func saveName(name: String , descriptions: String) {
    //1
    let appDelegate =
    UIApplication.sharedApplication().delegate as! AppDelegate

    let managedContext = appDelegate.managedObjectContext

    //2
    let entity =  NSEntityDescription.entityForName("Person",
        inManagedObjectContext:managedContext)

    let person = NSManagedObject(entity: entity!,
        insertIntoManagedObjectContext: managedContext)

    //3
    person.setValue(name, forKey: "name")
    person.setValue(descriptions, forKey: "description")

    //4
    do {
        try managedContext.save()
        //5
        people.append(person)
    } catch let error as NSError  {
        print("Could not save \(error), \(error.userInfo)")
    }
}
override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    //1
    let appDelegate =
    UIApplication.sharedApplication().delegate as! AppDelegate

    let managedContext = appDelegate.managedObjectContext

    //2
    let fetchRequest = NSFetchRequest(entityName: "Person")


    //3
    do {
        let results =
        try managedContext.executeFetchRequest(fetchRequest)
        people = results as! [NSManagedObject]
    } catch let error as NSError {
        print("Could not fetch \(error), \(error.userInfo)")
    }
}

}

1 个答案:

答案 0 :(得分:0)

如果要在单元格的同一文本字段中显示人员的姓名和说明,则应使用+指定。除此之外,您通常可以直接调用此人的姓名和描述:

person.name
person.description

试试这个:

cell!.textLabel!.text = person.name + " " + person.description
相关问题