ios - I am trying to use Core Data and load a table view with its contents in order and in sections -
i load, sections load, cant cells in each section load correctly. restarts beginning in each section duplicating each cell. entity name customer , holds following attributes. firstname, lastname, phonenumber,email, notes, , first. first first letter in last name use sections. can load, headers load start adding names same first letter in last name, starts messing up. let me know if need more code.
override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath?) -> uitableviewcell { //getsections() let cell = tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath!) let cust = customers[(indexpath?.section)!] print(indexpath?.row) let fname = cust.valueforkey("firstname") as? string print(fname) let lname = cust.valueforkey("lastname") as? string print(lname) cell.textlabel!.text = fname! + " " + lname! print("cell: \(cell.textlabel!.text)") return cell } override func viewdidload() { super.viewdidload() let appdelegate = uiapplication.sharedapplication().delegate as! appdelegate let managedcontext = appdelegate.managedobjectcontext context = managedcontext //2 let fetchrequest = nsfetchrequest(entityname:"customer") fetchrequest.sortdescriptors = [nssortdescriptor(key: "lastname", ascending: true)] //3 //var error: nserror? let fetchedresults = try! managedcontext.executefetchrequest(fetchrequest) as? [nsmanagedobject] if var results = fetchedresults { customers = results } else { print("could not fetch") } tableview.reloaddata() getsections() } func getsections() { var sections:[string] = [] for(var i=0;i<customers.count;i++) { let cust = customers[i] sections.append(cust.valueforkey("first") as! string) } sectionheaderstotal = sections let unique = array(set(sections)) sectionheaders = unique.sort() print(sectionheaders.count) //sectionheaders = unique //return unique.count }
take here approach read section-wise data -
http://www.andrewcbancroft.com/2015/03/05/displaying-data-with-nsfetchedresultscontroller-and-swift/ , here ios uitableview sections fetchedresultscontroller confusion
Comments
Post a Comment