iosswiftuitableviewtableviewswift-dictionary

Sectioning a tableview by dates from a dictionary


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?


Solution

  • Create a struct for the sections

    struct Section {
        let date : Date
        let meals : [Meal]
    }
    

    and map the grouped dictionary to an array of Section

    var sections = [Section]()
    
    let sortedDates = self.grouped.keys.sorted(>)
    sections = sortedDates.map{Section(date: $0, meals: self.grouped[$0]!)}
    

    You could add a date formatter to display the Date instance more meaningful.

    The table view data source methods are

    override func numberOfSections(in tableView: UITableView) -> Int {
        return sections.count
    }
    
    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {        
        return sections[section].date.description
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sections[section].meals.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "foodCell", for: indexPath)
        let meal = sections[indexPath.section].meals[indexPath.row]
        ...
    

    Note:

    Consider to use less optionals and a struct rather than a NSObject subclass. Unlike NSCoding Codable does not require to conform to NSObjectProtocol. And never declare properties as implicit unwrapped optional.