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 。歡迎大家訪問!
相關文章
- 刪除資料
- Swift實踐:使用CoreData儲存多種資料類的通訊錄Swift
- whk我【資料刪除】你個【資料刪除】的
- 資料儲存:CoreData
- kettle 實時同步資料(插入/更新/刪除資料)
- indexedDB 刪除資料Index
- 刪除elasticsearch資料Elasticsearch
- Flashback Drop閃回刪除功能實踐
- Laravel 如何實現資料的軟刪除Laravel
- indexedDB 刪除資料庫Index資料庫
- 2.11 刪除資料庫資料庫
- CoLab刪除資料夾
- MySQL刪除資料表MySql
- 刪除重複資料
- Laravel 中利用『模型事件』來實現刪除資料時的連帶刪除Laravel模型事件
- iOS CoreData (一) 增刪改查iOS
- CoreData - 簡單 增刪改查
- php 刪除資料夾的實現程式碼PHP
- MySQL 快速刪除大量資料(千萬級別)的幾種實踐方案——附原始碼MySql原始碼
- oracle刪除重資料方法Oracle
- python 刪除大表資料Python
- mongodb刪除重複資料MongoDB
- Linux 刪除資料夾命令Linux
- Laravel 資料庫裡的資料刪除Laravel資料庫
- 6.12php對資料庫的刪除和批量刪除PHP資料庫
- 資料夾刪除不了怎麼辦?資料夾刪除不了的解決方法
- 02-CoreData 的增刪改查
- iOS資料持久化儲存-CoreDataiOS持久化
- MongoDB資料庫中更新與刪除資料MongoDB資料庫
- sqlserver 億級資料刪除方案SQLServer
- sqlserver中刪除重複資料SQLServer
- 如何刪除Removable Drives資料夾?REM
- CnosDB的資料更新和刪除
- local資料夾能刪除嗎
- roaming資料夾可以刪除嗎
- ElasticSearch5.x 刪除資料ElasticsearchH5
- iOS程式碼瘦身實踐:刪除無用的類iOS
- Nestjs最佳實踐教程(七): 批次操作與軟刪除JS
- 誤刪除資料了怎麼辦?小編交易誤刪除資料的恢復方法