我是Swift的新人, 如何使用Swift在CoreData中使用MagicalRecord删除一行tableview。
我已经编写了这样的代码,但是没有用。我该如何解决这个问题。
override func tableView(tableView: UITableView, commitEditingStyle editingStyle :UITableViewCellEditingStyle,
forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
var aCantact=contactArray[indexPath.row]
aCantact.MR_deleteEntity()
contactArray.removeAtIndex(indexPath.row)
NSManagedObjectContext.MR_defaultContext().MR_saveOnlySelfAndWait()
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Bottom)
tableView.reloadData()
}
}
答案 0 :(得分:1)
试试这个
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
switch editingStyle {
case .Delete:
// remove the deleted item from the model
let appDel: AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
let context: NSManagedObjectContext = appDel.managedObjectContext!
context.deleteObject(contactArray[indexPath.row] as NSManagedObject)
contactArray.removeAtIndex(indexPath.row)
context.save(nil)
//tableView.reloadData()
// remove the deleted item from the `UITableView`
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
default:
return
}
}