johnny
johnny

Reputation: 37

Info or help on creating SwiftUI array that uses a model for data and user input for number of items in array?

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.

Upvotes: 0

Views: 36

Answers (0)

Related Questions