【问题标题】:How to delete a record in tableview with MagicalRecord CoreData in Swift如何在 Swift 中使用 MagicalRecord CoreData 删除 tableview 中的记录
【发布时间】:2015-03-15 19:13:33
【问题描述】:

我是 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()
        }

}

【问题讨论】:

    标签: ios swift ios8 magicalrecord


    【解决方案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
    
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-12
      • 1970-01-01
      • 2017-10-13
      • 2013-08-15
      • 1970-01-01
      相关资源
      最近更新 更多