2016-12-27 48 views
1

我創建了一個員工列表並顯示它tableView。用戶應該能夠在選擇刪除操作時刪除員工。我在一個數組上進行了測試,但是,當我將其更改爲核心數據時,它正在工作,它崩潰了。如果沒有無法使用tableView.deleteRowsAtIndexPaths與coreData

self.tableView.deleteRowsAtIndexPaths([indexPath],withRowAnimation:。自動)

該應用程序似乎工作,但刪除的行仍然具有空標籤存在(Without the name and title)

FavEmployeeViewController.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) 
} 

EmployeeStore.swift

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)") 
    } 
} 
+0

如果您點擊**(沒有名稱和標題)**,您應該能夠看到錯誤圖像。 – xxmilcutexx

+0

請勿刪除tableView行。刪除數據源中的行並重新加載表數據。 – zisoft

+0

在employeeStore.testCoreData.removeAtIndex(sourceIndexPath.row)後添加此行 – Chandan

回答

1

CoreData刪除對象後,您還需要從self.employeeStore陣列您剛纔刪除了它從CoreData刪除對象,以便添加self.employeeStore.remove(at: 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) 
+0

謝謝。它正在工作。問題是核心數據中的對象被刪除,但數組中的對象仍然存在。 – xxmilcutexx

+0

'self.employeeStore.removeAtIndex(indexPath.row)'我將它改爲_ //同時從Array self.employeeStore.testcoreData.removeAtIndex(indexPath.row)_中刪除該對象以使其工作。 – xxmilcutexx

+0

@xxmilcutexx歡迎隊友:) n編輯答案:) –