ios - Deleting a row from a tableView with multiple rows in section -
when try , delete row section multiple rows, when slide left reveal delete button , press it, deletes once tableview refreshed, visually nothing. if press again, deletes both rows in section. if dont press again , press , come viewcontroller, delete has worked animation never happened. deleting cell in section there 1 cell works perfectly. when there 2 cells or more in section, delegate functions never called commiteditingstyle does. let me know if need more code. thanks.
here nsfetchedresultscontrollerdelegate methods
override func tableview(tableview: uitableview, commiteditingstyle editingstyle: uitableviewcelleditingstyle, forrowatindexpath indexpath: nsindexpath) { let object = self.fetchedresultscontroller.objectatindexpath(indexpath) nsmanagedobject self.context.deleteobject(object) try! context.save() } func controllerwillchangecontent(controller: nsfetchedresultscontroller) { } func controllerdidchangecontent(controller: nsfetchedresultscontroller, didchangeobject anobject: anyobject, atindexpath indexpath: nsindexpath?, forchangetype type: nsfetchedresultschangetype, newindexpath: nsindexpath?) { var tableview = self.tableview uitableview switch(type) { case .delete: if let indexpath = indexpath { print("delete") tableview.deleterowsatindexpaths(nsarray(object:indexpath) as! [nsindexpath], withrowanimation: uitableviewrowanimation.fade) } break case .update: if let indexpath = indexpath { if let cell = tableview.cellforrowatindexpath(indexpath) { cell.textlabel!.text = (self.fetchedresultscontroller.valueforkey("firstname") as! string) + " " + (self.fetchedresultscontroller.valueforkey("lastname") as! string) print("update") } } default: break } } func controller(controller: nsfetchedresultscontroller, didchangesection sectioninfo: nsfetchedresultssectioninfo, atindex sectionindex: int, forchangetype type: nsfetchedresultschangetype) { switch(type) { case .delete: tableview.deletesections(nsindexset(index:sectionindex), withrowanimation: uitableviewrowanimation.fade) break default: break } } func controllerdidchangecontent(controller: nsfetchedresultscontroller) { }
solution: needed add self.tableview.reloaddata() @ end of commiteditingstyle function
override func tableview(tableview: uitableview, commiteditingstyle editingstyle: uitableviewcelleditingstyle, forrowatindexpath indexpath: nsindexpath) { let object = self.fetchedresultscontroller.objectatindexpath(indexpath) nsmanagedobject self.context.deleteobject(object) try! context.save() self.tableview.reloaddata() }
add self.tableview.reloaddata()
after delete row, or row technically deleted, table view has not been updated yet. see updated code below...
func controller(controller: nsfetchedresultscontroller, didchangesection sectioninfo: nsfetchedresultssectioninfo, atindex sectionindex: int, forchangetype type: nsfetchedresultschangetype) { switch(type) { case .delete: //this creates delete annimation tableview.deletesections(nsindexset(index:sectionindex), withrowanimation: uitableviewrowanimation.fade) //this deletes row array self.array.removeatindex(indexpath.row) //this reloads table view can see row deleted. self.tableview.reloaddata() break default: break } }
Comments
Post a Comment