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 。歡迎大家訪問!
相關文章
- CoreData實踐(五)——修改資料
- CoreData實踐(四)——查詢資料
- CoreData實踐(二)——設計資料結構資料結構
- CoreData實踐(一)
- 【RAC】刪除RAC資料庫節點(六)——刪除Oracle叢集服務資料庫Oracle
- CoreData實踐(三)——插入資料並使用SQLite Professional檢視SQLite
- Swift實踐:使用CoreData儲存多種資料類的通訊錄Swift
- 儲存系統實現-資料刪除之索引的刪除索引
- 資料儲存:CoreData
- Oc 資料庫CoreData資料庫
- kettle 實時同步資料(插入/更新/刪除資料)
- indexedDB 刪除資料Index
- Solr刪除資料Solr
- Flashback Drop閃回刪除功能實踐
- MongoDB 資料庫建立刪除、表(集合)建立刪除、資料增刪改查MongoDB資料庫
- Laravel 如何實現資料的軟刪除Laravel
- 資料夾刪除不掉怎麼辦?資料夾無法刪除
- MySQL刪除資料表MySql
- indexedDB 刪除資料庫Index資料庫
- 刪除重複資料
- MongoDB之資料刪除MongoDB
- 刪除大表資料
- git刪除資料夾Git
- 批次刪除資料方法
- 刪除重復資料
- Laravel 中利用『模型事件』來實現刪除資料時的連帶刪除Laravel模型事件
- 【Flashback】Flashback Drop閃回刪除功能實踐
- 【RAC】刪除RAC資料庫節點(一)——刪除資料庫例項資料庫
- 造資料難,刪除資料容易
- php 刪除資料夾的實現程式碼PHP
- 儲存系統實現-如何刪除資料
- 實驗2:利用BBED工具刪除行資料
- 【MySQL】刪除大量資料的具體實現MySql
- MySQL 快速刪除大量資料(千萬級別)的幾種實踐方案——附原始碼MySql原始碼
- iOS CoreData (一) 增刪改查iOS
- CoreData - 簡單 增刪改查
- 【RAC】刪除RAC資料庫節點(二)——刪除ASM資料庫ASM
- 【RAC】刪除RAC資料庫節點(五)——刪除ONS資料庫