如何在 Swift 中根据按钮单击更新文本值 [已关闭]

How to update the text values based on button click in Swift [closed]

提问人:Nus 提问时间:10/31/2023 最后编辑:Nus 更新时间:10/31/2023 访问量:89

问:


编辑问题以包括所需的行为、特定问题或错误以及重现问题所需的最短代码。这将帮助其他人回答这个问题。

19天前关闭。

我在swiftUI中有购物车应用程序。目前,当我单击“添加到购物车”按钮时,它能够将产品记录添加到视图中。但这就是问题.当我再次单击按钮两次相同的产品记录时,更直接的是重复记录和秒问题是,我也希望更新产品的数量,但我不确定该怎么做请建议我以击球的方式进行。

这是我的列表视图代码。

 import SwiftUI

struct ProductListView: View {
    
    var body: some View {

        NavigationStack {
            VStack {
                    if viewModel.productLists.count > 0 && !viewModel.refreshing {
 List(viewModel.productList, id: \.self) { product in
                    ProductListViewCell(productData: product)

                        } .listStyle(.grouped)
                    }
                }
            }
            .navigationTitle(Text("Product List"))
        }
    }
}

这是 cell 的代码。

struct ProductListViewCell: View {

    let productData: Product
    @EnvironmentObject var order: Order

    var body: some View {
        HStack {
            if let url = URL(string: productData.thumbnail) {
                ProductAsyncImageView(url: url)
                    .frame(width: 150, height: 150)
                    .mask(RoundedRectangle(cornerRadius: 16))
            }
            VStack(alignment: .leading,spacing: 5) {
                Text("Name: " +  (productData.title))
                    .frame(maxWidth: .infinity, alignment: .leading)
                    .font(.headline)

                Text("Description: " + (productData.description))
                    .frame(maxWidth: .infinity, alignment: .leading)
                    .multilineTextAlignment(.leading)

                Text("Price: £" + String(productData.price))
                    .frame(maxWidth: .infinity, alignment: .leading)
                    .font(.subheadline)

                Button {
                    order.add(item: productData)

                } label: {
                    HStack {
                        Image(systemName: "cart.badge.plus")

                        Text("Add to Cart")
                            .fontWeight(.semibold)
                    }
                    .foregroundColor(.white)
                    .frame(width: 150 , height: 40)

                }
                .background(Color(.systemBlue))
                .cornerRadius(10)
            }
        }
    }
}

这是我的订单类..

import SwiftUI

class Order: ObservableObject {

    @Published var product = [Product]()

    func add(item: Product) {
        product.append(item)
    }
    func remove(item: Product) {
        if let index = product.firstIndex(of: item) {
            product.remove(at: index)
        }
    }
}

这是订单查看的代码。

struct OrderView: View {

    @EnvironmentObject var order: Order

    var body: some View {
           NavigationStack {
               List {
                   Section {
                       ForEach(order.product) { item in
                           HStack(spacing: 20) {
                               if let url = URL(string: item.thumbnail) {
                                   ProductAsyncImageView(url: url)
                                       .frame(width: 90, height: 90)
                                        .padding(.bottom, -10)
                                       .clipShape(Circle())
                               }
                               VStack(alignment: .leading) {
                                   Text("Name: \(item.title)")
                                   Text("Price :£\(item.price)")
                                   Button("Show details") {

                                   }.foregroundColor(.gray)
                               }
                               HStack {
                                   Image(systemName: "multiply")
                                   Text("1")
                               }
                           }
                       }
                   }
                   Section {
                       NavigationLink("Place Order") {
                       }
                   }
               }
               .navigationTitle("Order")
           }
       }
   }

这是产品列表视图的屏幕截图。

enter image description here

这是我单击“添加到”按钮三次时具有重复记录的屏幕截图,但我只想要数量为 3 的单个记录。

enter image description here

iOS SwiftUI 购物车 修饰符

评论

0赞 jnpdx 10/31/2023
这需要一个最小的可重复示例。没有人可以运行此处包含的此代码
0赞 Nus 10/31/2023
还行。请问您要我在此处添加哪一部分?
1赞 Paulw11 10/31/2023
你还没有展示你的结构/类 - 这是你需要做工作的地方。调用该函数时,需要确定订单中是否已存在该物料,并递增数量或添加数量为 1 的新物料Orderorder.add()
0赞 Nus 10/31/2023
我已经使用类和产品列表视图更新了代码
2赞 jnpdx 10/31/2023
请阅读描述什么是最小可重现示例的页面 - 它将清楚地说明要包含哪些内容,不包含哪些内容,以便此处有足够的信息来运行代码,而不是掩盖问题的不相关代码。

答:

1赞 Pierre Janineh 10/31/2023 #1
  • 在课堂上,你应该有一个变量。Productquantity

  • 您应该检查数组中是否存在,并增加其 ,如下所示:Order.add(item:)Productproductsquantity

class Order: ObservableObject {

    @Published var products = [Product]()

    func add(item: Product) {
        var exists = false
        products.forEach { product in
            if product.id == item.id {
                exists = true
                product.quantity += 1
            }
        }
        
        if !exists {
            product.append(item)
        }
    }

    //The rest of your code...
}
  • 最后,您需要显示 in ,如下所示:quantityOrderView
...
    HStack {
        Image(systemName: "multiply")
        Text(item.quantity)
    }
...

修复了课堂上的错别字:Orderproduct:[Product] -> products:[Product]