TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Can someone help me with a stepper in a list

Forums > SwiftUI

I trying to make a list loaded from a struct with a stepper in it, and that stepper is needed to give the amount of every part. But I don't see it what I do wrong, but it don't work.

struct ConSumData: Identifiable,Codable, Comparable {
    var id = UUID()
    var artNr = ""
    var description = ""
    var favorite = ""
    let amount: Int

    static func < (lhs: ConSumData, rhs: ConSumData) -> Bool {
        lhs.description < rhs.description
    }
}

class Consum: ObservableObject {
    @Published var ConSumItem = [ConSumData]()

    init() {
        self.ConSumItem = []
    }
 }

struct ProductView: View {
    @ObservedObject var NewConsum: Consum

    @Environment(\.presentationMode) var presentationMode

        var body: some View {
                List {
                    SearchBar(text: $searchItem, placeholder: "Description")

                    if isLoaded == true {
                        ForEach (NewConsum.ConSumItem.filter({self.searchItem.isEmpty ? true: $0.description.contains(searchItem)})){ ConSumData in
                            HStack{
                                Text (ConSumData.favorite)
                                    .foregroundColor(.blue)
                                    .font(.title2)
                                Spacer()
                                VStack(alignment: .leading) {
                                    Text(ConSumData.description)
                                        .font(.headline)
                                    HStack {
                                        Text(ConSumData.artNr)
                                            .foregroundColor(.blue)
                                        Spacer()
                                        VStack {
                                            Stepper("Amount",value: ConSumData.amount, in: 0...100)
                                            .labelsHidden()
                                            Text("\(ConSumData.amount)")
                                        }
                                    }
                                }
                                Spacer()
                            }
                        }
                    }
                }
           }

2      

Does it compile and are errors displayed, because according to the Developer Documentation it should be UISearchBar, not SearchBar?

UISearchBar is part of the UIKit framework.

Also cannot see the declaration of searchitem, but I assume it is declared as a @State private var. Is that right?

However the Stepper function needs a State variable.


@State private var percentConsumed: Int = 0

// New update function
func updateValues() {
ConSumData.amount = percentConsumed
}

Stepper("Amount",value: $percentConsumed.onChange(updateValues), in: 0...100)
         .labelsHidden()
         Text("\(ConSumData.amount)")

I haven't tested it, and I hope it helps.

2      

Hi @Greenamberred, thanks for your comment but it don't work because the $percentConsumed.onChange is not possible. The searchbar is working, but I did not put it in the sample code because it was to much. ;-) If you have other idees for helping me, please put them here and I try.

2      

Maybe this might work, or something along these lines.


@State private var percentConsumed: Int = 0

Stepper("Amount", value: $percentConsumed, in: 0...100)
      .onChange(of: percentConsumed) { ConSumdata.amount in
            Text("\(ConSumData.amount)")
      }
      .labelsHidden()

2      

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.