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:

  1. you need use nsfetchedresultscontroller ("frc") - check out documentation here. see specify sectionnamekeypath when initialising frc.

  2. 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.

  3. in case, want month (and presumably year?) section name. there few ways that, easiest create function (eg. sectionidentifier) in sound class returns string section name, in format desire, derived datesaved. specify sectionnamekeypath:sectionidentifier when initialising frc.


Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -