2016-09-23 58 views
0

它正在從核心數據中刪除數據,但在deleteRowsAtIndexpaths上給出的錯誤是['無效的更新:在部分0中的行的無效數目。 update(5)必須等於更新之前該部分中包含的行數(5),加上或減去從該部分插入或刪除的行數(0插入,1刪除)以及正數或負數行移入或移出該部分(移入0,移出0)']無法從coredata實體中刪除對象swift

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
     if (editingStyle == UITableViewCellEditingStyle.Delete) 
     { 
      var Id = String() 
       if let RowData : NSDictionary = self.SavedJobs[indexPath.row] as? NSDictionary{ 
        Id = RowData["vacancy_id"] as! String 

       } 


      let OptionMenu = UIAlertController(title: "Alert", message: "Vacancy has been deleted", preferredStyle: .Alert) 
      let Okay = UIAlertAction(title: "Okay" , style: .Cancel, handler: { 
       (alert: UIAlertAction!) -> Void in 
       let appDelegate = 
        UIApplication.sharedApplication().delegate as! AppDelegate 

       let managedContext = appDelegate.managedObjectContext 
       print("saved data pre is \(self.SavedData)") 
       let Job = self.SavedData[indexPath.row] as NSManagedObject 
       managedContext.deleteObject(Job) 
       if (self.SavedData[indexPath.row].deleted){ 
       do{ 
        try managedContext.save() 
       } 
       catch{} 
       } 
       self.SavedJobs.removeObjectAtIndex(indexPath.row) 
       self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) 
       self.DeleteService(Id) 
       self.tableView.reloadData() 
      }) 
      OptionMenu.addAction(Okay) 
      self.presentViewController(OptionMenu, animated: true, completion: nil) 

     } 
    } 
+0

時請出示'numberOfRowsInSection'方法,但我的猜測是,你正在使用SavedData爲您的數據源,但上面的代碼刪除刪除來自SavedJobs的對象。 – pbasdf

+0

func tableView(tableView:UITableView,numberOfRowsInSection section:Int) - > Int { return SavedData.count } –

+0

我也在移除coredata對象嗎? managedContext.deleteObject(Job)by this command –

回答

0

您是否嘗試過使用beginUpdates和endUpdates?即

 self.tableView.beginUpdates() 
     self.SavedJobs.removeObjectAtIndex(indexPath.row) 
     self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) 
     self.DeleteService(Id) 
     self.tableView.endUpdates() 

而且,你不需要調用self.tableview.reloadData()使用deleteRowsAtIndexPaths

+0

...你不需要'beginUpdates/endUpdates'一次刪除操作。 – vadian