Post

Replies

Boosts

Views

Activity

Custom Initializer Binding Problems
I'm trying to add sodas from ListView to ContentView and have that data passed on and update the view in AisleView. I'm having a lot of trouble getting my columnOne and columnTwo properties to update the AisleView correctly. I know the data is being passed to AisleView, but it isn't updating the actual view. I believe the issue is when I'm trying to initialize my columns. Any help would be appreciated. Thank you! class Inventory { var inventory: [Product] = [ Product(name: "Coke", price: 2.99, aisle: 1, location: 10), Product(name: "Pepsi", price: 3.99, aisle: 1, location: 6), Product(name: "Dr. Pepper", price: 1.99, aisle: 2, location: 8), Product(name: "Pibb", price: 1.50, aisle: 2, location: 1) ] } struct ListView: View { @State var base = Inventory() @State var sodas: [Product] = [] var body: some View { VStack { ContentView(sodas: $sodas) List { ForEach(base.inventory) { product in HStack { Text(product.name) Spacer() Text("\(product.price, specifier: "%.2f")") Button { sodas.append(product) } label: { Image(systemName: "plus") } } } } } } } struct ListView_Previews: PreviewProvider { static var previews: some View { ListView() } } struct ContentView: View { @Binding var sodas: [Product] @State private var columnOne: [Product] = [] @State private var columnTwo: [Product] = [] init(sodas: Binding<[Product]>) { self._sodas = sodas self.columnOne = aisleSort(sodas: self.sodas, aisle: 1) self.columnTwo = aisleSort(sodas: self.sodas, aisle: 2) } var body: some View { VStack { HStack(spacing: 10) { AisleView(products: $columnOne) AisleView(products: $columnTwo) } } } func aisleSort(sodas: [Product], aisle: Int) -> [Product] { var sort: [Product] = [] for soda in sodas { if soda.aisle == aisle { sort.append(soda) } } return sort } } struct AisleView: View { @Binding var products: [Product] @State private var buttonNumber = 0 var location: [Int] { var answer: [Int] = [] for number in products { answer.append(number.location) } return answer } func idicator(number: Int) -> Color { if location.contains(number) { return Color.red } else { return Color.primary } } var body: some View { ZStack { VStack(alignment: .center, spacing: 0) { ForEach(1..<21, id: \.self) {number in ZStack { if location.contains(number) { Button { buttonNumber = number } label: { HStack { Rectangle() .foregroundColor(.red) .frame(width: 20, height: 20) } } .overlay(buttonNumber == number ? InfoView(sodas: products, number: number).offset(x: -70) : nil) } else { HStack { Rectangle() .frame(width: 20, height: 20) .foregroundColor(idicator(number: number)) Text("\(number)") } } } } } } } } struct InfoView: View { @State var sodas: [Product] @State var number: Int var body: some View { VStack { ForEach(sodas) { soda in if soda.location == number { ZStack { RoundedRectangle(cornerRadius: 10) .foregroundColor(.clear) .background(.regularMaterial) .clipShape(RoundedRectangle(cornerRadius: 10)) VStack { Text(soda.name) Text("$\(soda.price, specifier: "%.2f")") } // .font(.title) } .frame(width: 100, height:60) } } } } } struct AisleView_Previews: PreviewProvider { static var previews: some View { AisleView(products: ListView().$sodas) } } `
5
0
485
Sep ’23
View won't update in ForEach loop
I'm trying make it so that when the plus button is pushed in ListView the price will increment by one and be reflected on the screen. I'm not getting any errors, and I'm getting the expected result from the print statement, but the view will not update. I've tried just incrementing the price directly in the button as opposed to creating a variable for the same thing. but I get an error saying the left side is immutable because it's a let constant. Any help would be appreciated, I'm getting really frustrated. Thank you! struct Product: Identifiable { let id = UUID() var name: String var price: Double var aisle: Int var location: Int } class Inventory { var inventory: [Product] = [ Product(name: "Coke", price: 2.99, aisle: 1, location: 10), Product(name: "Pepsi", price: 3.99, aisle: 1, location: 6), Product(name: "Dr. Pepper", price: 1.99, aisle: 2, location: 8), Product(name: "Pibb", price: 1.50, aisle: 2, location: 1) ] } struct ListView: View { @State var base = Inventory().inventory @State var userList: [Product] = [] var body: some View { List { ForEach(base) { product in var thing = product.price HStack { Text(product.name) Spacer() Button { thing += 1 print(thing) } label: { Image(systemName: "plus") } Text("\(thing, specifier: "%.2f")") } } } } } struct ListView_Previews: PreviewProvider { static var previews: some View { ListView() } }
2
0
1.3k
Aug ’23