0

I have a database consisting of two tables. One holds MenuItem data and the other MenuCategory data. I written a simple SQL query to return all of the data from each table and then I've used this to decode the information into my own data models:

struct MenuCategory: Codable {
    var categoryID: Int
    var categoryName: String
}

struct MenuItem: Codable {
    let itemID: Int
    let itemCategory: String
    let itemName: String
    let itemPrice: Double
    let itemVegan: Bool
    let itemVegetarian: Bool
    let itemGlutenFree: Bool
    let itemPrepTime: Int
    let itemDescription: String
    let itemStatus: String
    let itemStockCount: Int
}

For my MenuViewController() I wanted to present the menuItems in sections based on their menuCategory and so made a property to hold an array of each type and this has all been fine up to the point of using indexPath.section and indexPath.row. I can easily use return menuCategories.count for numberOfSections and return the categoryName for titleForHeaderInSection but obviously I want the cellForRowAt function to return the appropriate menuItems for that section and do this dynamically as per categoryName.

class MenuViewController {
    var menuItems = [MenuItem]()
    var menuCategories = [MenuCategory]()
  
    func numberOfSections(in tableView: UITableView) -> Int {
        return menuCategories.count
    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return menuCategories[section].categoryName
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return menuItems.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: PropertyKeys.menuTableViewCell, for: indexPath)
        let menuItem = menuItems[indexPath.row]

        cell.textLabel?.text = menuItem.itemName
        return cell
    }
}

I've removed a lot of code from the above to save cluttering up post but I'm unsure about how I use two different custom types here and have them working together well or if I need to be using two at all and I'm over thinking this?! I've tried using a nested array but can't seem to use two different types for this method. I've also tried to create a dictionary using menuCategory names as the keys and then the menuItems as the values such as: var dictionary = [String: [MenuItem]]. I've then thought, what about adding a [menuItem]? property to my MenuCategory struct as an optional and then after receiving the data I could set the newly added menuItems: [MenuItem]? property of each MenuCategory to the menuItem that has the corresponding itemCategory. This just sent me in a back and forth for loop, faffing for about 2 hours and still not getting it. There is referential integrity from the database so I know the values of the .itemCategory for the MenuItem instances do match those found for the .categoryName property for MenuCategory instances. If anyone has reached the end of this, i'd appreciate being pointed in the right direction/point out something I've overlooked. Thanks.

colinsgone
  • 11
  • 4
  • Explain what those two datasets represent and how they belong under one list. – El Tomato Jun 24 '21 at 00:14
  • @ElTomato it's a list of items for a menu and it needs to contain the items for each category in a section of said list with a section title of the category name – colinsgone Jun 24 '21 at 00:39
  • You are still not explaining how the two datasets interact with each other and how they belong under one roof. – El Tomato Jun 24 '21 at 01:07
  • @ElTomato I don't understand what specifics you're asking for in that case? They relate to each other in the sense that each MenuItem belongs to a MenuCategory. For example "Stella Beer" might belong in the "Drinks" category. To be clear - The TableView would be a list of all of these items that are sectioned off by category and the section titles will be the category names and so naturally, the menuItems that would classify as "drinks" would appear in the "drinks" section. I am using two databases which I have said may not be necessary, hence posting here to find out. – colinsgone Jun 24 '21 at 01:12
  • They are probably related to each other only in your mind, but they aren't as far as others see the MenuCategory model and the MenuItem model. – El Tomato Jun 24 '21 at 01:17
  • @ElTomato for further clarity if required - The data models here also match the tables from the external database. The database has referential integrity with a one to many relationship where one menuCategory can have many menuItems. – colinsgone Jun 24 '21 at 01:17
  • @ElTomato Does it matter how they are related? Regardless of the relation, the question I'm looking to find an answer for is "How to use two custom types whilst still using indexPath.section and indexPath.row for UITableView". Example: How can I use my menuItem data model for the rows according to my sections that are set by my secondary data model: menuCategory. – colinsgone Jun 24 '21 at 01:21

0 Answers0