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.