简体   繁体   English

在表视图中删除核心数据对象

[英]Deleting core data objects in a table view

I am new to Swift and I am following a tutorial about making a gesture-driven app. 我是Swift的新手,我正在学习有关制作手势驱动应用程序的教程。

As in the tutorial, a table view is populated from array objects. 如本教程中一样,表视图是从数组对象中填充的。

There is a function to remove an item from the table view when the user drags the cell to the left. 当用户将单元格向左拖动时,具有从表格视图中删除项目的功能。

func toDoItemDeleted(toDoItem: ToDoItem) {
  let index = (toDoItems as NSArray).indexOfObject(toDoItem)
  if index == NSNotFound { return }

  // could removeAtIndex in the loop but keep it here for when indexOfObject works
  toDoItems.removeAtIndex(index)

  // use the UITableView to animate the removal of this row
  tableView.beginUpdates()
  let indexPathForRow = NSIndexPath(forRow: index, inSection: 0)
  tableView.deleteRowsAtIndexPaths([indexPathForRow], withRowAnimation: .Fade)
  tableView.endUpdates()    
}

In the tutorial the array is named toDoItems , and there is also a NSObject class named ToDoItem . 在本教程中,数组名为toDoItems ,还有一个名为ToDoItem的NSObject类。

In my implentation, I am using Core Data instead of an array to populate the table view. 我正努力地使用Core Data而不是数组来填充表视图。 I have create a NSObject named Tasks instead of the NSObject from the tutorial. 我已经创建了一个名为Tasks的NSObject,而不是本教程中的NSObject。

How should I change the function to remove the Core Data object instead of the array object? 如何更改函数以删除核心数据对象而不是数组对象?

Thank you. 谢谢。

Your problem is that you are removing the NSManagedObject from the array, but not from the managed object context. 您的问题是您要从阵列中删除NSManagedObject,而不是从托管对象上下文中删除。 To remove it from Core Data, you should remove it from your managed object context like so: 要从核心数据中删除它,您应该像这样从托管对象上下文中删除它:

managedObjectContext?.deleteObject(toDoItem)
managedObjectContext?.save(nil)

However, if you are using Core Data to populate a UITableView, I would advise using NSFetchedResultsController. 但是,如果您使用Core Data填充UITableView,则建议使用NSFetchedResultsController。 A complete implementation can be found here . 一个完整的实现可以在这里找到。

  1. Delete Item from Core Data Core Data删除项目
  2. Delete Item from your NSObject Array NSObject数组中删除项目
  3. Remove That Row from UITableView UITableView删除该Row

Write Below Code In commitEditingStyle Method commitEditingStyle方法中编写以下代码

if editingStyle == .Delete {

       // Step : 1
        let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate

        let manageContext = appDelegate.managedObjectContext!
        // I have Store Item in Data which is type of NSManagedObject
        // This will remove item from coredata
        manageContext.deleteObject(Data[indexPath.row])

        var err:NSError?

        if !manageContext.save(&err)
        {
           println("Data couldn't delete")
        }
        // Step : 2 Delete the row from the data source

        self.Data.removeAtIndex(indexPath.row)

        // Step 3:
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
    }   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM