CoreData實踐(五)——修改資料

乞力馬紮羅的雪CYF發表於2015-09-17

     我在前面兩篇部落格實現瞭如何插入資料,和如何查詢資料,現在在前面的基礎上,要實現更新資料。

(1)UsersTableViewController中的實現如下:

import UIKit
import CoreData

class UsersTableViewController: UITableViewController {

  var dataArr:Array<AnyObject>! = []
  var context:NSManagedObjectContext!
  
    override func viewDidLoad() {
        super.viewDidLoad()

      
      context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
      
    refreshData()
      
      
    }

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

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Potentially incomplete method implementation.
        // Return the number of sections.
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete method implementation.
        // Return the number of rows in the section.
        return dataArr.count
    }

  
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell

      
      var name = dataArr[indexPath.row].valueForKey("name") as! String
      var age = dataArr[indexPath.row].valueForKey("age") as! Int

      var label = cell.viewWithTag(101) as! UILabel
      label.text = "姓名:\(name);  年齡:\(age)"
      
      
      

        return cell
    }
  
  override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    
    var data  = dataArr[indexPath.row] as! NSManagedObject
    
    var vc = storyboard?.instantiateViewControllerWithIdentifier("UserContent") as! UserContentViewController
    
    vc.data = data
    
    presentViewController(vc, animated: true, completion: nil)
    
  }
  
  
  func refreshData(){
  
    var f = NSFetchRequest(entityName: "Users")
    dataArr = context.executeFetchRequest(f, error: nil)
    
    tableView.reloadData()
    
  }
  
  override func viewWillAppear(animated: Bool) {
    refreshData()
  }

}

(2)然後在UserContentViewController中的實現如下:

import UIKit
import CoreData

class UserContentViewController: UIViewController {

  var data:NSManagedObject!
  
  
  
  @IBOutlet weak var nameTextField: UITextField!
  @IBOutlet weak var ageTextField: UITextField!
  
    override func viewDidLoad() {
        super.viewDidLoad()

      nameTextField.text = data.valueForKey("name") as! String
      
      var age: AnyObject? = data.valueForKey("age")
      
      ageTextField.text = "\(age!)"

      
    }

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

  @IBAction func submitButton(sender: UIButton) {
    
    data.setValue(nameTextField.text, forKey: "name")
    data.setValue(ageTextField.text.toInt(), forKey: "age")
    
    data.managedObjectContext?.save(nil)
    
    dismissViewControllerAnimated(true, completion: nil)
  }


  @IBAction func cancelButton(sender: UIButton) {
    
    dismissViewControllerAnimated(true, completion: nil)
  }
  
  
}

(3)其中UserContentViewController對應的佈局如圖所示:


(4)最後執行程式,能夠通過在輸入框中修改姓名和年齡,來修改資料庫中的值。


github主頁:https://github.com/chenyufeng1991  。歡迎大家訪問!


相關文章