I have an Object like this (simplified):
struct BoxModel: Codable, Identifiable, Hashable {
var id: Int
var number: Int
}
I have a two dimensional array of these:
@State private var matrix = generate()
where generate creates a 2D array of the objects: [[BoxModel]]. I then display them in a grid. The grid is pretty complex but here's a very simplified form:
LazyVGrid(columns: columns, spacing: 0 ) {
ForEach(1...9, id: \.self) { j in
Text("\(getModel(j).number)")
}
}
Where getValue gets the item from the array that corresponds to the grid location. Since the grid is complex, I need to do some arithmetic in a separate function to do this.
Below this is a button. When the user taps it, it changes the number property of one of the objects in the array. However, the number doesn't update. I thought it would since the array is a @State property.
Is there a way to make it update?
Here's an extremely simplified form of the ContentView:
struct ContentView: View {
@State private var matrix = generate()
var body: some View {
NavigationView {
GeometryReader { geometry in
ScrollView {
VStack {
let columns = [GridItem(.fixed(50), spacing: 0),
GridItem(.fixed(50), spacing: 0),
GridItem(.fixed(50), spacing: 0)]
LazyVGrid(columns: columns, spacing: 0 ) {
ForEach(1...9, id: \.self) { j in
Text("\(getModel(j).number)")
}
}
.padding()
Button {
var model = getModel(0)
model.number = 5
} label: {
Text("Click Me")
}
}
}
}
}
}
func getModel(_ i: Int) -> BoxModel {
// very simplified
return matrix[0][i]
}