簡體   English   中英

無法從CoreData實體Swift中刪除對象

[英]unable to delete object from coredata entity swift

它正在從核心數據中刪除數據,但是在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)

        }
    }

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

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

另外,使用deleteRowsAtIndexPaths時,您無需調用self.tableview.reloadData()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM