I'm a new developer and I have a finance app with a collection view of categories.
When the user taps on a category, the next page should populate a collection view of subcategories for the main category.
Here is my current data model:
import UIKit
struct Category {
var name: String
var color: UIColor
var amountBudgeted: Double
var subCategories: [SubCategory]
}
struct SubCategory {
var name: String
var amountBudgeted: Double
}
let categories = [
Category(
name: "Income",
color: UIColor(rgb: Constants.green),
amountBudgeted: 0.00,
subCategories: [
SubCategory(name: "Paycheck", amountBudgeted: 0.00),
SubCategory(name: "Bonus", amountBudgeted: 0.00),
SubCategory(name: "Dividend", amountBudgeted: 0.00),
SubCategory(name: "Rental Income", amountBudgeted: 0.00),
]),
Category(
name: "Housing",
color: UIColor(rgb: Constants.grey),
amountBudgeted: 0.00,
subCategories: [
SubCategory(name: "Mortgage", amountBudgeted: 0.00),
SubCategory(name: "Property Tax", amountBudgeted: 0.00),
SubCategory(name: "HOA Fees", amountBudgeted: 0.00),
SubCategory(name: "Household Repairs", amountBudgeted: 0.00),
]),
Category(
name: "Transportation",
color: UIColor(rgb: Constants.red),
amountBudgeted: 0.00,
subCategories: [
SubCategory(name: "Car Payment", amountBudgeted: 0.00),
SubCategory(name: "Gas", amountBudgeted: 0.00),
SubCategory(name: "Car Repairs", amountBudgeted: 0.00),
SubCategory(name: "Registration", amountBudgeted: 0.00),
]),
Category(
name: "Food",
color: UIColor(rgb: Constants.yellow),
amountBudgeted: 0.00,
subCategories: [
SubCategory(name: "Groceries", amountBudgeted: 0.00),
SubCategory(name: "Restaurants", amountBudgeted: 0.00),
]),
]
I can easily call the main category properties in cellForItemAt like cell.nameLabel.text = categories[indexPath.item].name
, but I can't get the subcategory view to work.
I'm trying cell.nameLabel.text = categories[indexPath.item].subCategories[indexPath.item].name
but that isn't working properly.
I also need to figure out how to relate the data between view controllers, but that might need to be a separate question.
Do I need to rethink my data model? Or do I need to set the cellForItemAt method differently?
Thanks.