I have got a dictionary where it stores an array meals and date uploaded by the user. Now I want to show a table view of all the meals stored and sectioned by their date.
How do I set up the numberOfSections(), titleForHeader(), numberOfRowsInSection() and cellForRowAt() methods required?
Below is what I current have and does not work
var grouped = Dictionary<Date, [Meal]>()
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return grouped.keys.count
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return Array(grouped)[section].key as! String
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
var count = Array(grouped.keys)
print(count)
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return Array(grouped.values)[section].count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "foodCell", for: indexPath)
let meal = Array(grouped.values)[indexPath.row]
cell.textLabel?.text = meal.foodname //error here
cell.detailTextLabel?.text = String(meal.calories) + "kJ"
cell.detailTextLabel?.textColor = UIColor.secondaryLabel
cell.accessoryType = .disclosureIndicator
return cell
}
and my Meal object
class Meal: NSObject, Codable {
var id: String?
var foodname: String?
var date: Date?
}