ios - NSFetchedResultsController swift sections -
i have table view takes data coredata entity 3 fields. firstname: string, lastname: string , done:nsnumber (which uiswitch can turned on or off).
i want populate table first , last names first section names having switch on, , second section names having switch off.
here code saves data:
class viewcontroller: uiviewcontroller { var context = (uiapplication.sharedapplication().delegate as! appdelegate).managedobjectcontext! var newitem: list? = nil @iboutlet weak var txtfirst: uitextfield! @iboutlet weak var txtlast: uitextfield! @iboutlet weak var switchdone: uiswitch! override func viewdidload() { super.viewdidload() } override func didreceivememorywarning() { super.didreceivememorywarning() } @ibaction func canceltapped(sender: anyobject) { dismissvc() } @ibaction func savetapped(sender: anyobject) { let context = self.context let ent = nsentitydescription.entityforname("list", inmanagedobjectcontext: context) let nitem = list(entity: ent!, insertintomanagedobjectcontext: context) nitem.firstname = txtfirst.text nitem.lastname = txtlast.text nitem.done = switchdone.on context.save(nil) dismissvc() } func dismissvc() { navigationcontroller?.popviewcontrolleranimated(true) } }
and here code populates table view:
class tableviewcontroller: uitableviewcontroller, nsfetchedresultscontrollerdelegate { var context = (uiapplication.sharedapplication().delegate as! appdelegate).managedobjectcontext! var frc: nsfetchedresultscontroller = nsfetchedresultscontroller() func getfetchedresultscontroller() -> nsfetchedresultscontroller { frc = nsfetchedresultscontroller(fetchrequest: listfetchrequest(), managedobjectcontext: context, sectionnamekeypath: nil, cachename: nil) return frc } func listfetchrequest() -> nsfetchrequest { let fetchrequest = nsfetchrequest(entityname: "list") let sortdescriptor = nssortdescriptor(key: "firstname", ascending: true) fetchrequest.sortdescriptors = [sortdescriptor] return fetchrequest } override func viewdidload() { super.viewdidload() frc = getfetchedresultscontroller() frc.delegate = self frc.performfetch(nil) } override func didreceivememorywarning() { super.didreceivememorywarning() } func controllerdidchangecontent(controller: nsfetchedresultscontroller) { tableview.reloaddata() } // mark: - table view data source override func numberofsectionsintableview(tableview: uitableview) -> int { let numberofsections = frc.sections?.count return numberofsections! } override func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { let numberofrowsinsection = frc.sections?[section].numberofobjects return numberofrowsinsection! } override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath) as! uitableviewcell let list = frc.objectatindexpath(indexpath) as! list cell.textlabel?.text = list.firstname cell.detailtextlabel?.text = list.lastname return cell } }
i can't figure out how separate data 2 sections based on 'done' attribute.
first, need sort list
objects done
attribute, firstname
:
let donesortdescriptor = nssortdescriptor(key: "done", ascending: true) let sortdescriptor = nssortdescriptor(key: "firstname", ascending: true) fetchrequest.sortdescriptors = [donesortdescriptor, sortdescriptor]
then amend fetched results controller use done
attribute define sections:
frc = nsfetchedresultscontroller(fetchrequest: listfetchrequest(), managedobjectcontext: context, sectionnamekeypath: "done", cachename: nil)
finally, implement tableview:titleforheaderinsection
set appropriate title each section.
Comments
Post a Comment