Hello!
I am trying to create a view in which the user has selected multiple objects and for each of those selected objects (in this case, “Chair”, “Desk”, and “Table”), as the screen appears, there are 2 sliders where they are able to adjust binding values for 2 different properties, as shown below.
The problem I am trying to work out is how to dynamically set these binding values for each respective object, because when I declare for example @State var sellPrice and bind that to the sliders as Slider(value: $sellPrice, obviously every “sell price” slider is going to move together as I move one of them. Given the code below, how would you suggest I structure these bindings to be unique to each object?
‘’’
@Environment(.dismiss) var dismiss
@Environment(.managedObjectContext) private var viewContext
@FetchRequest(
entity: Items.entity(),
sortDescriptors: [NSSortDescriptor(keyPath: \Items.itemName, ascending: true)]
)
var items: FetchedResults<Items>
@FetchRequest(
entity: Office.entity(),
sortDescriptors: [NSSortDescriptor(keyPath: \Office.officeName, ascending: true)]
)
var officeArray: FetchedResults<Office>
@Binding var editOffice: String
@State var sellPrice: Double = 500
@State var cost: Double = 200
var body: some View {
VStack(alignment: .leading) {
Text("Enter Item Info")
.font(.title)
.bold()
.padding(.horizontal)
Form {
List {
//Group {
ForEach(officeArray.filter {$0.wrappedOfficeName == editOffice}) { office in
ForEach(office.itemArray, id: \.self) {itemList in
Text(getItemName(id: itemList))
.font(Font.custom("Avenir Heavy", size: 14))
Text("Sell Price: \(sellPrice, specifier: "%.0f")")
.foregroundColor(.white)
Slider(value: $sellPrice, in: 0.0...3500.0, step: 1.0, onEditingChanged: {_ in
print(Text("\(sellPrice)"))})
Text("Cost: \(cost, specifier: "%.0f")")
.foregroundColor(.white)
Slider(value: $cost, in: 0.0...3500.0, step: 1.0, onEditingChanged: {_ in
print(Text("\(cost)"))})
}
‘’’
Thanks!