I have been stuck on this error for quite some time now. I have a UITabBar
with 2 tabs. The first one contains a UITableViewController
with meals on different dates of the week. The second tab contains a UIViewController
with a login screen.
The UITableViewController
is called 'EettafelView'. It gets it's meals (using Alamofire) from an online source and presents them in the application. They are cached in CoreData
and retrieved using a NSFetchedResultsController
to present them. For this i'm using the default UITableViewDelegates
(numberOfRowsInSection, numberOfSections etc) - If they are relevant i'll post them in an edit.
When the tableview loads and i have set the fetchController and have performed a fetch i pretty-print its objects to test what it holds and if the sort-descriptors have done their job. This leads to the following:
Hollandse stoof met bokbier - Optional("maandag, 20 feb.")
Gehaktballetjes
Quorn gehakt
Stamppot van verse spinazie - Optional("dinsdag, 21 feb.")
Rookworst
Kaasspies
Indiase Curry - Optional("woensdag, 22 feb.")
Kip
Bloemkool
Pasta Mexicane - Optional("donderdag, 23 feb.")
Gehakt
Tofu en Bruine Bonen
Boeuf des Gardiens - Optional("vrijdag, 24 feb.")
Rundvlees
Kastanjechampignons
With pretty-print code:
if let objects = fetchController?.fetchedObjects as? [Gerecht] {
for object in objects {
print("\(object.basis ?? "") - \(object.dateString)")
print("\t\(object.vlees ?? "")")
print("\t\(object.vega ?? "")")
}
}
This is exactly how it is supposed to be. Thus the FetchController holds the right objects and has them sorted correctly. The view however looks likes this:
The section titles are wrong. The code to retrieve the section titles is as follows:
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if let sections = fetchController?.sections, sections.count > 0 {
return sections[section].name
} else {
return nil
}
}
Which is I think as generic as it can be. What can be the problem of it retrieving the wrong section titles?
EDIT: - CellForRow Method
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Eettafel") as? Eettafel_Cell
let gerecht = fetchController?.object(at: indexPath) as? Gerecht
if let gerecht = gerecht {
cell?.gerecht = gerecht
}
return cell!
}
EDIT - Gerecht in cellForRow
printing the description of the NSManagedObject (breakpoint at if-let gerecht) gives:
Printing description of gerecht.some:
<Almanapp.Gerecht: 0x1740b77c0> (entity: Gerecht; id: 0xd0000000000c0000 <x-coredata://A6B5411D-DF77-43DE-8084-86C76C42F68A/Gerecht/p3> ; data: {
basis = "Hollandse stoof met bokbier";
date = "2017-02-20 00:23:07 +0000";
dateString = "maandag, 20 feb.";
status = nil;
vega = "Quorn gehakt";
vlees = Gehaktballetjes;
})
Which is correct.
Try replacing your titleForHeaderInSection function for this.
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if let gerecht = fetchController?.object(at: IndexPath(row: 0, section: section)) as? Gerecht {
return gerecht.dateString
}else{
return nil
}
}
I think the problem is you were using:
if let sections = fetchController?.sections, sections.count > 0 {
return sections[section].name
Check that and tell me please