简体   繁体   English

无法从CoreData实体Swift中删除对象

[英]unable to delete object from coredata entity swift

it is deleting data from core data but on deleteRowsAtIndexpaths it gives error which is ['Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (5) must be equal to the number of rows contained in that section before the update (5), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'] 它正在从核心数据中删除数据,但是在deleteRowsAtIndexpaths它给出的错误是['无效更新:第0节中的行数无效。更新(5)之后现有节中包含的行数必须等于(0)。该部分在更新之前包含的行(5),加上或减去从该部分插入或删除的行数(已插入0,已删除1),以及加上或减去移入或移出该部分的行数(已移动0)进入,0移出)。']

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)

        }
    }

Have you tried using beginUpdates and endUpdates? 您是否尝试过使用beginUpdates和endUpdates? ie

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

Also, you dont need to call self.tableview.reloadData() when using deleteRowsAtIndexPaths 另外,使用deleteRowsAtIndexPaths时,您无需调用self.tableview.reloadData()

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

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