CoreData实践(六)——数据删除

     我在前面几篇博客讲到了如何使用CoreData来进行插入,查询,更新操作。现在我们将要实现删除操作,其实删除操作非常简单。具体实现如下:

(1)在UserTableViewController中重写两个方法,具体实现如下:

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()
    
    
  }
  
  // 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()
  }
  
  
  override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    
    //这里返回true,表示cell可以编辑;
    
    
    return true
  }
  
  override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    
    if editingStyle == UITableViewCellEditingStyle.Delete{
      
      context.deleteObject(dataArr[indexPath.row] as! NSManagedObject)
      
      //一定要执行保存操作,否则不会删除;
      context.save(nil)
      
      refreshData()
      
    }else if editingStyle == UITableViewCellEditingStyle.Insert{
      
      
    }
    
  }
  
}

(2)运行程序,向左拖动cell,就可以删除一条数据。


github主页:https://github.com/chenyufeng1991  。欢迎大家访问!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值