I am using NSFetchResultController to show sections in my table. I got a NSDate attribute to sort by date. I am fetching the data correctly, my issue is that I want the sections to show the month of the year like July, August. Currently the dates are showing up like the image below.
FetchRequest
var fetchResultController: NSFetchedResultsController = { () -> NSFetchedResultsController<Gratitude> in
let fetchRequest = NSFetchRequest<Gratitude>(entityName: "Gratitude")
let sortByDate = NSSortDescriptor(key: "date", ascending: false)
fetchRequest.sortDescriptors = [sortByDate]
let frc = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: Context.shared.persistentContainer.viewContext, sectionNameKeyPath: "date" , cacheName: nil)
return frc
}()
Header
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
//return g2[section].monthLabel.uppercased()
let sectionInfo = fetchResultController.sections?[section]
return sectionInfo?.name
}
numberOfRowsInSection
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let count = fetchResultController.sections?[section].numberOfObjects{
return count
}
return 0
}
numberOfSections
func numberOfSections(in tableView: UITableView) -> Int {
if let sections = fetchResultController.sections?.count {
return sections
}
return 0
}
Any help would be appreciated.
Updated Image