I am using FRC and I want to create sections that groups data according to date (DD MMMM)! Each task has a date and i am using that date and formatting it for the section header titles.
I am using Apple's sample code that they have provided in Objective C and converted it in to swift. Here is the code:
import UIKit
import CoreData
class completedNotes: NSManagedObject
{
@NSManaged var cNote: String
@NSManaged var cPriorityColor: UIColor
@NSManaged var cDate: Date
}
extension completedNotes {
var sectionIdentifier : String? {
// Create and cache the section identifier on demand.
self.willAccessValue(forKey: "sectionIdentifier")
var tmp = self.primitiveValue(forKey: "sectionIdentifier") as? String
self.didAccessValue(forKey: "sectionIdentifier")
if tmp == nil {
if let timeStamp = self.value(forKey: "cDate") as? NSDate {
/*
Sections are organized by month and year. Create the section
identifier as a string representing the number (year * 1000) + month;
this way they will be correctly ordered chronologically regardless
of the actual name of the month.
*/
let calendar = NSCalendar.current
let components = calendar.dateComponents([.year, .month,], from: timeStamp as Date)
tmp = String(format: "%ld", components.year! * 1000 + components.month!)
self.setPrimitiveValue(tmp, forKey: "sectionIdentifier")
}
}
return tmp
}
}
The above code is in NSManagedObject where I am creating a transient property sectionIdentifier. It takes value of cDate which is NSManagedObject.
Then in titlesforheaderinSection
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
struct Formatter {
static let formatter : DateFormatter = {
let fmt = DateFormatter()
let dateFormat = DateFormatter.dateFormat(fromTemplate: "MMMM yyyy", options: 0,
locale: NSLocale.current)
fmt.dateFormat = dateFormat
return fmt
}()
}
if let theSection = fetchedResultsController1.sections?[section] as? NSFetchedResultsSectionInfo,
let numericSection = Int(theSection.name) {
var components = DateComponents()
components.year = numericSection / 1000
components.month = numericSection % 1000
if let date = Calendar.current.date(from: components) {
let titleString = Formatter.formatter.string(from: date)
return titleString
}
}
return nil
}
This is the code I am using for other tableview functions:
func numberOfSections(in tableView: UITableView) -> Int {
return (fetchedResultsController1.sections?.count)!
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return fetchedResultsController1.sections![section].numberOfObjects
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellid, for: indexPath) as! TasksTableViewCell
cell.selectionStyle = .none
cell.textview.text = fetchedResultsController1.object(at: indexPath).cNote
cell.priorityline.backgroundColor = fetchedResultsController1.object(at: indexPath).cPriorityColor
return cell
}
This is NSFetchedResultsController:
let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.moContext1, sectionNameKeyPath: "sectionIdentifier", cacheName: nil)
aFetchedResultsController.delegate = self
_fetchedResultsController = aFetchedResultsController as? NSFetchedResultsController<completedNotes>
Why am I not able to get any sections to show up in the tableview and why when I remove the last tableview cell, the app crashes?
Thanks!
The key path passed to the sectionNameKeyPath:
argument of the fetched results controller must be visible to the
Objective-C runtime. In Swift 4 that requires an explicit annotation
with @objc
(compare also How can I deal with @objc inference deprecation with #selector() in Swift 4?):
extension completedNotes {
@objc var sectionIdentifier : String? {
// ...
}
}
Without that, the section name key path is silently ignored.