ios - How to group tableView data using sectionNameKeyPath (Swift) -
i've not been able figure out how following in swift , looking pointers.
i'm pulling string values core data , displaying them in uitableview...that's working , displaying entries. want 'group' these values date created (also stored in core data) having hard time implementing group header...here's have:
func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { var sound = self.sounds[indexpath.row] var cell = uitableviewcell() // convert date string var formatter: nsdateformatter = nsdateformatter() formatter.dateformat = "yyyy-mm-dd-hh-mm-ss" let datetimeprefix: string = formatter.stringfromdate(sound.datesaved) // display following in each rows cell.textlabel!.text = sound.name + " saved: " + datetimeprefix return cell } could point me in right direction how implement 'headers'? have tableview in main.storyboard setup 'grouped' style.
--- update
thanks response below but, unfortunately, examples in obj-c , i'm shooting swift.
to expand, have following retrieving data core data , displaying in uitableview:
override func viewdidload() { super.viewdidload() self.tableview.datasource = self self.tableview.delegate = self } override func viewwillappear(animated: bool) { super.viewwillappear(animated) var context = (uiapplication.sharedapplication().delegate appdelegate).managedobjectcontext! var request = nsfetchrequest(entityname: "sound") self.sounds = context.executefetchrequest(request, error: nil)! [sound] } func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { return self.sounds.count } func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let cell: uitableviewcell = uitableviewcell(style: uitableviewcellstyle.subtitle, reuseidentifier: "mytestcell") var sound = self.sounds[indexpath.row] // convert date string var formatter: nsdateformatter = nsdateformatter() formatter.dateformat = "yyyy-mm-dd @ hh:mm:ss" let datetimeprefix: string = formatter.stringfromdate(sound.datesaved) // display following in each rows cell.textlabel!.text = sound.name // sound name // display following each subtitles cell.detailtextlabel!.text = datetimeprefix // sound duration (currently 'date') return cell } could point me in right direction how go 'grouping' each month 'month' header using sectionnamekeypath, believe? specifically, piece of code need modified break list 'sections'.
here pointers:
you need use
nsfetchedresultscontroller("frc") - check out documentation here. see specifysectionnamekeypathwhen initialising frc.there boilerplate code updating tableview using frc. easiest way this, , observe how frc works, create project using apple's master/detail application template, ensuring select "use core data" option. play in project see how works, , cut/paste own project.
in case, want
month(and presumablyyear?) section name. there few ways that, easiest create function (eg.sectionidentifier) insoundclass returns string section name, in format desire, deriveddatesaved. specifysectionnamekeypath:sectionidentifierwhen initialising frc.
Comments
Post a Comment