Ios 无法将tableView.deleteRowsAtIndexPaths与coreData一起使用

Ios 无法将tableView.deleteRowsAtIndexPaths与coreData一起使用,ios,swift,uitableview,core-data,swift2.3,Ios,Swift,Uitableview,Core Data,Swift2.3,我已经创建了一个员工列表,并显示它tableView。用户应能够在选择删除操作时删除员工。我在一个阵列上测试了它,它工作正常,但是当我把它改成核心数据时,它崩溃了。没有 self.tableView.deleteRowsAtIndexPaths([indexPath],withRowAnimation:.Automatic) 应用程序似乎正常工作,但删除的行仍然存在,且标签为空( faveEmployeeViewController.swift //MARK:- Table Functions

我已经创建了一个员工列表,并显示它
tableView
。用户应能够在选择删除操作时删除员工。我在一个阵列上测试了它,它工作正常,但是当我把它改成核心数据时,它崩溃了。没有

self.tableView.deleteRowsAtIndexPaths([indexPath],withRowAnimation:.Automatic)

应用程序似乎正常工作,但删除的行仍然存在,且标签为空(

faveEmployeeViewController.swift

//MARK:- Table Functions
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    if searchController.active && searchController.searchBar.text != "" {
        return filteredEmployees.count
    }
    //coredata
    return employeeStore.testCoreData.count
}

//MARK:- TEST coredata
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    // Create an instance of UITableViewCell, with default appearance
    let cell = tableView.dequeueReusableCellWithIdentifier("EmployeeCell", forIndexPath: indexPath) as! EmployeeCell
    cell.updateLabels()

    let item = employeeStore.testCoreData[indexPath.row]

    cell.nameLabel.text = item.valueForKey("name") as? String
    cell.jobPosition.text = item.valueForKey("title") as? String

    return cell

}


//MARK:- Edit bar Button
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    // If the table view is asking to commit a delete command...
    if editingStyle == .Delete {
        let item = employeeStore.testCoreData[indexPath.row]
        let title = "Delete \(item.valueForKey("name") as! String)?"
        let message = "Are you sure you want to delete this item?"

        let ac = UIAlertController(title: title, message: message, preferredStyle: .ActionSheet)
        let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
        ac.addAction(cancelAction)

        let deleteAction = UIAlertAction(title: "Delete", style: .Destructive,
                                         handler: { (action) -> Void in
                                            // Remove the item from the store
                                            debugPrint("index", indexPath.row)
                                            self.employeeStore.testremoveEmployee(indexPath.row)
                                            // Also remove that row from the table view with an animation
                                            self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
                                                                                            debugPrint("index", indexPath.row)
        })
        ac.addAction(deleteAction)

        // Present the alert controller
        presentViewController(ac, animated: true, completion: nil)
    }
}

override func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
    // Update the model
    employeeStore.moveEmployeeAtIndex(sourceIndexPath.row, toIndex: destinationIndexPath.row)
}
import UIKit
import CoreData

class EmployeeStore {
    //MARK:-Coredata
    var testCoreData = [NSManagedObject]() //storing favs entities

...

//MARK:- Test delete coreData -> removeFavEmployee
func testremoveEmployee(index: Int) {
    debugPrint("testremoveEmployee")
    //retrieve from CoreData
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let managedContext = appDelegate.managedObjectContext
    let employee = testCoreData[index]

    let _: NSError! = nil
    do {
        managedContext.deleteObject(employee)
        debugPrint("manage2delete")
        try managedContext.save()

    } catch {
        print("error : \(error)")
    }
}
EmployeeStore.swift

//MARK:- Table Functions
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    if searchController.active && searchController.searchBar.text != "" {
        return filteredEmployees.count
    }
    //coredata
    return employeeStore.testCoreData.count
}

//MARK:- TEST coredata
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    // Create an instance of UITableViewCell, with default appearance
    let cell = tableView.dequeueReusableCellWithIdentifier("EmployeeCell", forIndexPath: indexPath) as! EmployeeCell
    cell.updateLabels()

    let item = employeeStore.testCoreData[indexPath.row]

    cell.nameLabel.text = item.valueForKey("name") as? String
    cell.jobPosition.text = item.valueForKey("title") as? String

    return cell

}


//MARK:- Edit bar Button
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    // If the table view is asking to commit a delete command...
    if editingStyle == .Delete {
        let item = employeeStore.testCoreData[indexPath.row]
        let title = "Delete \(item.valueForKey("name") as! String)?"
        let message = "Are you sure you want to delete this item?"

        let ac = UIAlertController(title: title, message: message, preferredStyle: .ActionSheet)
        let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
        ac.addAction(cancelAction)

        let deleteAction = UIAlertAction(title: "Delete", style: .Destructive,
                                         handler: { (action) -> Void in
                                            // Remove the item from the store
                                            debugPrint("index", indexPath.row)
                                            self.employeeStore.testremoveEmployee(indexPath.row)
                                            // Also remove that row from the table view with an animation
                                            self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
                                                                                            debugPrint("index", indexPath.row)
        })
        ac.addAction(deleteAction)

        // Present the alert controller
        presentViewController(ac, animated: true, completion: nil)
    }
}

override func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
    // Update the model
    employeeStore.moveEmployeeAtIndex(sourceIndexPath.row, toIndex: destinationIndexPath.row)
}
import UIKit
import CoreData

class EmployeeStore {
    //MARK:-Coredata
    var testCoreData = [NSManagedObject]() //storing favs entities

...

//MARK:- Test delete coreData -> removeFavEmployee
func testremoveEmployee(index: Int) {
    debugPrint("testremoveEmployee")
    //retrieve from CoreData
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let managedContext = appDelegate.managedObjectContext
    let employee = testCoreData[index]

    let _: NSError! = nil
    do {
        managedContext.deleteObject(employee)
        debugPrint("manage2delete")
        try managedContext.save()

    } catch {
        print("error : \(error)")
    }
}

您还需要从
self.employeeStore
数组中删除刚刚从
CoreData
中删除的对象,因此在从
CoreData
中删除对象后,添加
self.employeeStore.remove(位于:indexPath.row)

self.employeeStore.testremoveEmployee(indexPath.row)

// Also remove that object from Array
self.employeeStore. testcoreData.removeAtIndex(indexPath.row)

// Now remove that row from the table view with an animation
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)

如果单击(没有名称和标题),您应该能够看到错误的图像。不要删除tableView行。删除数据源中的行并重新加载表数据。在employeeStore.testCoreData.removeAtIndex(sourceIndexPath.row)之后添加此行谢谢。它正在工作。问题是核心数据中的对象已被删除,但数组中的对象仍然存在。
self.employeeStore.removatendex(indexath.row)
我将其更改为//同时从数组self.employeeStore.testcoreData.removatendex(indexath.row)中删除该对象,使其工作。@xxmilcutexx欢迎伴侣:)n编辑的答案:)