swift - UITableView row won't removed after being deleted -
i'm having problem after had deleted tableview
row, row won't removed, here code, had follow tutorial online, , delete data model, won't dismiss deleted row unless unwind previous screen , view, why it? :
func tableview(tableview: uitableview, caneditrowatindexpath indexpath: nsindexpath) -> bool { return true } func tableview(tableview: uitableview, commiteditingstyle editingstyle: uitableviewcelleditingstyle, forrowatindexpath indexpath: nsindexpath) { if (editingstyle == uitableviewcelleditingstyle.delete) { let product = frc.objectatindexpath(indexpath) as! product let productname = product.name let message = (mclocalization.sharedinstance.stringforkey("cart_remove_one_item_message", replacements: ["%s" : productname!])) let alert = uialertcontroller(title: (mclocalization.sharedinstance.stringforkey("cart_remove_title")), message: message, preferredstyle: uialertcontrollerstyle.alert) let okaction = uialertaction(title: "ok", style: uialertactionstyle.default, handler: { (_)in let managedobject : nsmanagedobject = self.frc.objectatindexpath(indexpath) as! nsmanagedobject self.moc.deleteobject(managedobject) self.tableview.reloaddata() { try self.moc.save() } catch { print("failed save.") return } }) alert.addaction(okaction) alert.addaction(uialertaction(title: "cancel", style: uialertactionstyle.cancel, handler: nil)) self.presentviewcontroller(alert, animated: false, completion: nil) } }
you need remove object array reload tableview
after moc.save()
successfully.
let managedobject : nsmanagedobject = self.frc.objectatindexpath(indexpath) as! nsmanagedobject self.moc.deleteobject(managedobject) { try self.moc.save() } catch { print("failed save.") return }
edit: need add in question using nsfetchedresultscontroller
, think have set delegate self.frc.delegate = self
, add delegate method remove reloaddata
there no need now.
func controllerwillchangecontent(controller: nsfetchedresultscontroller) { tableview.beginupdates() } func controllerdidchangecontent(controller: nsfetchedresultscontroller) { tableview.endupdates() } func controller(controller: nsfetchedresultscontroller, didchangeobject anobject: anyobject, atindexpath indexpath: nsindexpath?, forchangetype type: nsfetchedresultschangetype, newindexpath: nsindexpath?) { switch type { case .insert: tableview.insertrowsatindexpaths([newindexpath!], withrowanimation: .automatic) case .delete: tableview.deleterowsatindexpaths([indexpath!], withrowanimation: .automatic) default: break } }
fore more detail nsfetchedresultscontroller check tutorials you.
Comments
Post a Comment