ios - Swift: Deleting core data, table won't update -
this code seems deleting actual data ok table doesn't update without row unless force close app , reopen. in advance.
import uikit import coredata
class viewcontroller: uiviewcontroller,uitableviewdatasource,uitableviewdelegate {
var lists = [nsmanagedobject]() var deletelistindexpath: nsindexpath? = nil @iboutlet weak var mytableview: uitableview! @ibaction func additem(sender: anyobject) { let alert = uialertcontroller(title: "new list", message: "add new list", preferredstyle: .alert) let saveaction = uialertaction(title: "save", style: .default) { (uialertaction) -> void in let textfield = alert.textfields![0] uitextfield if textfield != "" { self.saveitem(textfield.text!) self.mytableview.reloaddata() } } let cancelaction = uialertaction(title: "cancel", style: .default, handler: nil) alert.addtextfieldwithconfigurationhandler(nil) alert.addaction(saveaction) alert.addaction(cancelaction) presentviewcontroller(alert, animated: true, completion: nil) } func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { return lists.count } func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let mycell:uitableviewcell = mytableview.dequeuereusablecellwithidentifier("prototype1")! let list = lists[indexpath.row] print(lists[indexpath.row]) mycell.textlabel?.text = list.valueforkey("listtitle") as? string return mycell } override func viewdidload() { super.viewdidload() self.mytableview.datasource = self self.mytableview.delegate = self let appdelegate = uiapplication.sharedapplication().delegate as! appdelegate let managedcontext = appdelegate.managedobjectcontext let fetchrequest = nsfetchrequest(entityname:"list") // may need sort list let sortdescriptor1 = nssortdescriptor(key: "listtitle", ascending: true) let sortdescriptors = [sortdescriptor1] fetchrequest.sortdescriptors = sortdescriptors var fetchedresults : [nsmanagedobject] = [] { fetchedresults = try managedcontext.executefetchrequest(fetchrequest) as! [nsmanagedobject] } catch { return } list in fetchedresults { lists.append(list) } self.mytableview.reloaddata() } func tableview(tableview: uitableview, caneditrowatindexpath indexpath: nsindexpath) -> bool { return true } func tableview(tableview: uitableview, commiteditingstyle editingstyle: uitableviewcelleditingstyle, forrowatindexpath indexpath: nsindexpath) { if(editingstyle == .delete ) { // find logitem object user trying delete let logitemtodelete = lists[indexpath.row] let appdelegate = uiapplication.sharedapplication().delegate as! appdelegate let managedcontext = appdelegate.managedobjectcontext managedcontext.deleteobject(logitemtodelete) self.mytableview.reloaddata() } } func saveitem(saveitem: string) { let appdelegate = uiapplication.sharedapplication().delegate as! appdelegate let managedcontext = appdelegate.managedobjectcontext let entity = nsentitydescription.entityforname("list", inmanagedobjectcontext: managedcontext) let item = nsmanagedobject(entity: entity!, insertintomanagedobjectcontext: managedcontext) item.setvalue(saveitem, forkey: "listtitle") { try managedcontext.save() lists.append(item) } catch { print("error") } }
}
i think still need remove "logitem" lists object.
lists.removeatindex(indexpath.row)
i give more precise answer if upload code function @ tableview datasource protocol:
override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath)
Comments
Post a Comment