CoreData实践(六)——数据删除
2015-09-17 19:48
218 查看
我在前面几篇博客讲到了如何使用CoreData来进行插入,查询,更新操作。现在我们将要实现删除操作,其实删除操作非常简单。具体实现如下:
(1)在UserTableViewController中重写两个方法,具体实现如下:
(2)运行程序,向左拖动cell,就可以删除一条数据。
github主页:https://github.com/chenyufeng1991 。欢迎大家访问!
(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 。欢迎大家访问!
相关文章推荐
- 第三周项目(5):建设“顺序表”算法库
- ReentrantLock Condition 用法
- 拆分组件名
- CodeForces 12C - Fruits
- 史上最浅显易懂的并查集算法
- STL源码——RB-Tree insert操作
- IOS UI学习 UI 十个小控件 初度学习
- 第三次作业
- HDU 5447 Good Numbers (2015年长春赛区网络赛K题)
- java中如何获取一个正整数的位数?
- Javscript中的null和undefined
- Android学习笔记之——WebView
- 锁对象Lock-同步问题更完美的处理方式
- ajax上传文件struts2后台接收处理
- JavaScript基础系列--函数
- HDU 2012 素数判定
- No module ata_piix found的解决方法
- ubuntu 怎么安装搜狗输入法
- aerospik工具集——备份和还原
- boost python object creation process