ios - Core Data empty tableview cell every time view loads -
i having problem tableview. every time view loads, creates new empty cell in table view. class view controller is: class mygoalsscreen: uiviewcontroller, uitableviewdelegate, uitableviewdatasource,nsfetchedresultscontrollerdelegate {
var controller: nsfetchedresultscontroller<goal>! var goals = [goal]() @iboutlet weak var tableview: uitableview! @iboutlet weak var totalgold: uilabel! override func viewdidload() { super.viewdidload() tableview.delegate = self tableview.datasource = self attemptfetch() //generatetestdata() // additional setup after loading view. } @ibaction func prepareforunwind(segue:uistoryboardsegue){ } override func unwind(for unwindsegue: uistoryboardsegue, towardsviewcontroller subsequentvc: uiviewcontroller) { let segue = unwindscalesegue(identifier:unwindsegue.identifier, source:unwindsegue.source, destination:unwindsegue.destination) segue.perform() } func tableview(_ tableview: uitableview, caneditrowat indexpath: indexpath) -> bool { return true } func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecell(withidentifier: "goalcell", for: indexpath) as! goalcell configurecell(cell: cell, indexpath: indexpath nsindexpath) return cell } func configurecell(cell: goalcell,indexpath: nsindexpath){ let goal = controller.object(at: indexpath indexpath) cell.configurecell(goal: goal) } func tableview(_ tableview: uitableview, commit editingstyle: uitableviewcelleditingstyle, forrowat indexpath: indexpath) { if editingstyle == uitableviewcelleditingstyle.delete{ let managedobject: nsmanagedobject = controller.object(at: indexpath) nsmanagedobject; context.delete(managedobject) ad.savecontext() } } func tableview(_ tableview: uitableview, titlefordeleteconfirmationbuttonforrowat indexpath: indexpath) -> string? { return "finish" } func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int { if let sections = controller.sections{ let sectioninfo = sections[section] return sectioninfo.numberofobjects } return 0 } func numberofsections(in tableview: uitableview) -> int { if let sections = controller.sections{ return sections.count } return 0 } func tableview(_ tableview: uitableview, heightforrowat indexpath: indexpath) -> cgfloat { return 188 } func attemptfetch(){ let fetchrequest : nsfetchrequest<goal> = goal.fetchrequest() let datesort = nssortdescriptor(key: "created", ascending: false) fetchrequest.sortdescriptors = [datesort] let controller = nsfetchedresultscontroller(fetchrequest: fetchrequest, managedobjectcontext: context, sectionnamekeypath: nil, cachename: nil) controller.delegate = self self.controller = controller do{ try controller.performfetch() } catch{ let error = error nserror print("\(error)") } } func tableview(_ tableview: uitableview, didselectrowat indexpath: indexpath) { if let objs = controller.fetchedobjects, objs.count > 0{ let goal = objs[indexpath.row] performsegue(withidentifier: "newgoalscreen", sender: goal) } } override func prepare(for segue: uistoryboardsegue, sender: any?) { if segue.identifier == "newgoalscreen"{ if let destination = segue.destination as? newgoalscreen{ if let goal = sender as? goal{ destination.goaltoedit = goal } } } } func attemptfetch2(){ let fetchrequest: nsfetchrequest<goal> = goal.fetchrequest() do{ self.goals = try context.fetch(fetchrequest) } catch{ print(error) } } func controllerwillchangecontent(_ controller: nsfetchedresultscontroller<nsfetchrequestresult>) { tableview.beginupdates() } func controllerdidchangecontent(_ controller: nsfetchedresultscontroller<nsfetchrequestresult>) { tableview.endupdates() } func controller(_ controller: nsfetchedresultscontroller<nsfetchrequestresult>, didchange anobject: any, @ indexpath: indexpath?, type: nsfetchedresultschangetype, newindexpath: indexpath?) { switch(type){ case.insert: if let indexpath = newindexpath{ tableview.insertrows(at: [indexpath], with: .fade) } break case.delete: if let indexpath = indexpath{ tableview.deleterows(at: [indexpath], with: .fade) } break case.update: if let indexpath = indexpath{ tableview.reloadrows(at: [indexpath], with: uitableviewrowanimation.automatic) } break default: break } }
and configurecell function have: class goalcell: uitableviewcell {
@iboutlet weak var title: uilabel! @iboutlet weak var difficulty: uilabel! @iboutlet weak var desc: uilabel! @iboutlet weak var frequency: uilabel! func configurecell(goal: goal){ title.text = goal.title desc.text = goal.desc frequency.text = goal.frequency difficulty.text = goal.difficulty }
}
Comments
Post a Comment