I have a dictionary that contains a key/value pair of a Date that contains an array of my custom object Meals grouped together by the same dates.
Meal Object:
class Meal: NSObject, Codable {
var id: String?
var foodname: String?
var quantity: Float!
var brandName: String?
var quantityType: String?
var calories: Float!
var date: Date?
}
In my TableView:
var grouped = Dictionary<Date, [Meal]>()
var listOfAllMeals = [Meal]() //already populated
self.grouped = Dictionary(grouping: self.listOfAllMeals.sorted(by: { ($0.date ?? nilDate) < ($1.date ?? nilDate) }),
by: { calendar.startOfDay(for: $0.date ?? nilDate) })
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return grouped.count
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return Array(grouped.keys)[section] as! String //this throws a thread error
}
This allows users to upload a meal multiple times a day for future viewing and now I want to show the meals in a TableView sectioned by their dates and sorted already by the latest. How do I achieve that?
Create a struct for the sections
and map the grouped dictionary to an array of
SectionYou could add a date formatter to display the
Dateinstance more meaningful.The table view data source methods are
Note:
Consider to use less optionals and a struct rather than a
NSObjectsubclass. UnlikeNSCodingCodabledoes not require to conform toNSObjectProtocol. And never declare properties as implicit unwrapped optional.