它正在從核心數據中刪除數據,但在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)
}
}
時請出示'numberOfRowsInSection'方法,但我的猜測是,你正在使用SavedData爲您的數據源,但上面的代碼刪除刪除來自SavedJobs的對象。 – pbasdf
func tableView(tableView:UITableView,numberOfRowsInSection section:Int) - > Int { return SavedData.count } –
我也在移除coredata對象嗎? managedContext.deleteObject(Job)by this command –