0

I need to create a view with a list. The number of items in the list is called here "Number of Stands"

enter image description here

import Foundation
import SwiftUI

struct Stand: Codable, Identifiable, Hashable {
    var id: String {
        return self.stand
    }
    var stand: String
    let area: String
    let speed: Int
    var rf: Int
}


extension Stand {
    static let Mock_Stand = Stand(stand: "32", area: "2.4", speed: 0, rf: 0)
}

After hitting "lets go" i go to this screen.

enter image description here

Here is the code for my list row.

import SwiftUI

struct StandRowView: View {
    let stand: String
    @Binding var speedCurrent: String
    let rfactor: String

    var body: some View {
        VStack(alignment:.leading){
            HStack{
                Text("Stand")
                Text(stand)
                TextField("Enter Speed", text: $speedCurrent)
                    .frame(width: 110, height: 40)
                    .foregroundColor(.black)
                    .multilineTextAlignment(.center)
                    .background()
                    .border(Color.gray, width: 5)
                    .cornerRadius(10)
                    .padding(.trailing, 30)
                Text(rfactor)
            }
            .padding()
        }
    }
}

Here is the code for this screen currently.

struct Speeds1View: View {

let title: String
let productArea: String
let noStands: String
let billetArea: String
@Environment(\.dismiss) var dismiss
@State var txtspeedCurrent: String
@State private var rF = ""
@State private var standNo: Int = 0
@State private var rFCalculated: Int = 0

var body: some View {
    NavigationStack{
            ZStack{
                List{
                    HStack(alignment: .center){
                        Spacer()
                    VStack(){
                            Text("# of Stands")
                            Text(self.noStands)
                            Text("Billet Area")
                            Text(self.billetArea)
                            Text("Product Area")
                            Text(self.productArea)
                            Text(" Reduction Ratio: \(RedRatio) : 1")
                            Text("Testing")
                            Text(" Ave. Area Red: \(AveAreaRed)%")
                        }
                        Spacer()
                    }

                    ForEach(0..<standCount, id: \.self) { standCount in
                        StandRowView(stand:"\(standCount + 1)", speedCurrent: $txtspeedCurrent, rfactor: "Area1/Area2")
                    }

                    .padding(.horizontal)
                    .listRowInsets(EdgeInsets())
                    .listRowSeparator(.hidden)
                }
            }

        .toolbar{
            ToolbarItem(placement: .navigationBarLeading) {
                Button(action: {
                    dismiss()
                }, label: {
                    Image(systemName: "arrowshape.turn.up.backward")
                        .font(.footnote)
                        .foregroundColor(.blue)
                })
            }
            ToolbarItem(placement: .navigationBarTrailing) {
                Button(action: {
                    //                    updateText()
                    //                    dismiss()
                }, label: {
                    Image(systemName: "square.and.arrow.up")
                        .resizable()
                        .frame(width: 20, height: 25)
                        .foregroundStyle(Color.blue, Color.gray)
                })
            }
        }
    }
    .navigationBarTitleDisplayMode(.inline)
    .navigationTitle("Calculate Area, \(title)")
}
}

With this code my stands are not separated by ID. i get this when i begin to type in something for "Speed".

enter image description here

so.. i need to separate each row by stand no. then i need to be able to do a .onchange so that i can create a formula to calculate Stand 1 area / Stand 2 area and update the text on right but that will be easy i think.

any help showing me a similar project online i can reference would be a huge help.

7
  • Where's the code for the StandRowView? You probably need to pass the standCount (from the ForEach) in as a unique ID of some sort. Commented Mar 3, 2024 at 5:19
  • Please create a minimal, reproducible example Commented Mar 3, 2024 at 7:26
  • Your problem is mainly caused by not having a proper data source or rather not having one at all. You do have a struct but you aren’t using it. You should create an array of your struct and use that in your ForEach instead of looping over an index Commented Mar 3, 2024 at 7:59
  • create an array of my struct then use in my for each. ill look that up. thanks Commented Mar 3, 2024 at 12:56
  • well, my problem is i dont know how many are in my array until someone types in a stand count. how do i say make "5" stand rows in my list using this struct "Stand"? and make the ID the number of stand iterated. any example i find online has nothing related to this. hoping someone here may have seen an example like this Commented Mar 3, 2024 at 13:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.