Swiftui 单元格选择器文本字段不可点击

Swiftui 单元格选择器文本字段不可点击,swiftui,uiscrollview,picker,Swiftui,Uiscrollview,Picker,我试图显示一个滚动视图,其中每个单元格都有3个选择器文本字段。 但是,只有最后一个单元格具有可点击的工作选择器文本字段 当我查看BasketListCell预览时,它正是我想要的。所有选择器都能很好地处理占位符。但在scrollview中,情况并非如此 我在BasketList中有3个不同的listData,但ShoppingCartView将第一个重复3次 这是我的单元格结构篮,每个选择器文本字段有3个Int struct Basket: Identifiable { var id: Int

我试图显示一个滚动视图,其中每个单元格都有3个选择器文本字段。 但是,只有最后一个单元格具有可点击的工作选择器文本字段

当我查看BasketListCell预览时,它正是我想要的。所有选择器都能很好地处理占位符。但在scrollview中,情况并非如此

我在BasketList中有3个不同的listData,但ShoppingCartView将第一个重复3次

这是我的单元格结构篮,每个选择器文本字段有3个Int

struct Basket: Identifiable {
var id: Int {
    return elementID
}
var elementID: Int
var image: String
var title: String
var brand: String
var price: String
var selectionImage: String
var discountedPrice: String
var sizeSelectedIndex: Int?
var colorSelectedIndex: Int?
var itemSelectedIndex : Int?
}

这是我的演示篮单,包含3个元素

    struct BasketList {
    static let listData: [Basket] = [
            Basket(elementID: 0, image: "Tee", title: "3-Stripes Shirt", brand: "Adidas Original", price: "$79.40", selectionImage: "checkmark", discountedPrice: "$48.99", sizeSelectedIndex: 0, colorSelectedIndex: 0, itemSelectedIndex: 0),
            Basket(elementID: 0, image: "Blouse", title: "Tuxedo Blouse", brand: "Lost Ink", price: "$50.00", selectionImage: "checkmark", discountedPrice: "$34.90", sizeSelectedIndex: 0, colorSelectedIndex: 0, itemSelectedIndex: 0),
            Basket(elementID: 0, image: "Tee", title: "Linear Near Tee", brand: "Converse", price: "$28.50", selectionImage: "checkmark", discountedPrice: "$19.99", sizeSelectedIndex: 0, colorSelectedIndex: 0, itemSelectedIndex: 0)
        ]
}
那是我的篮球牢房

struct BasketListCell: View {
@State var isSelected: Bool = false
@State private var itemSelectedIndex : Int?
@State private var sizeSelectedIndex : Int?
@State private var colorSelectedIndex: Int?
var colors = ["Black", "Green", "Red", "Blue"]
var sizes = ["XS", "S", "M", "L", "XL", "XXL"]
var items = ["1", "2", "3", "4", "5", "6"]
var item: Basket
var body: some View {
HStack(spacing: 20) {
            ZStack {
                PickerTextField(data: sizes, placeHolder: "Size", lastSelectedIndex: self.$sizeSelectedIndex)
                    .overlay(
                        Image(systemName: "chevron.down")
                    )
            }
            .frame(width: UIScreen.main.bounds.width / (375/100), height: 44, alignment: .center)
            
            ZStack{
                PickerTextField(data: colors, placeHolder: "Color", lastSelectedIndex: self.$colorSelectedIndex)
                    .overlay(
                        Image(systemName: "chevron.down")
                    )
                
            }
            .frame(width: UIScreen.main.bounds.width / (375/100), height: 44, alignment: .center)
            
            ZStack {
                PickerTextField(data: items, placeHolder: "Item", lastSelectedIndex: self.$itemSelectedIndex)
                    .overlay(
                        Image(systemName: "chevron.down")
                    )
            }
            .frame(width: UIScreen.main.bounds.width / (375/100), height: 44, alignment: .center)
        }
       }
最后,这是我的购物车视图

struct ShoppingCartView: View {
@State var selectedProduct = Basket(elementID: 0, image: "", title: "", brand: "", price: "", selectionImage: "", discountedPrice: "", sizeSelectedIndex: 0, colorSelectedIndex: 0, itemSelectedIndex: 0)
@State var shown: Bool = false
var body: some View {
    ZStack {
        Color(.white)
            .edgesIgnoringSafeArea(.all)
        VStack {
            NavigationView {
                ScrollView(.vertical, showsIndicators: false) {
                    VStack(spacing: 20) {
                        ForEach(BasketList.listData) { item in
                            BasketListCell(item: item)
                                .onTapGesture {
                                    self.shown = true
                                    self.selectedProduct = item
                                }
                        }
                    }
                }
如果你能更好地检查这些图像,你就能清楚地理解我的问题

这是BasketListCell预览

那是我的购物车

struct ShoppingCartView: View {
@State var selectedProduct = Basket(elementID: 0, image: "", title: "", brand: "", price: "", selectionImage: "", discountedPrice: "", sizeSelectedIndex: 0, colorSelectedIndex: 0, itemSelectedIndex: 0)
@State var shown: Bool = false
var body: some View {
    ZStack {
        Color(.white)
            .edgesIgnoringSafeArea(.all)
        VStack {
            NavigationView {
                ScrollView(.vertical, showsIndicators: false) {
                    VStack(spacing: 20) {
                        ForEach(BasketList.listData) { item in
                            BasketListCell(item: item)
                                .onTapGesture {
                                    self.shown = true
                                    self.selectedProduct = item
                                }
                        }
                    }
                }

elementID
模型类
Basket
的属性必须是唯一的。您当前有3个
对象,所有对象都具有重复的
标识符
,导致swiftUI每次都读取第一个对象。将其更改为一些唯一的值可以解决此问题

当前-:

struct BasketList {
    static let listData: [Basket] = [
            Basket(elementID: 0, image: "Tee", title: "3-Stripes Shirt", brand: "Adidas Original", price: "$79.40", selectionImage: "checkmark", discountedPrice: "$48.99", sizeSelectedIndex: 0, colorSelectedIndex: 0, itemSelectedIndex: 0),
            Basket(elementID: 0, image: "Blouse", title: "Tuxedo Blouse", brand: "Lost Ink", price: "$50.00", selectionImage: "checkmark", discountedPrice: "$34.90", sizeSelectedIndex: 0, colorSelectedIndex: 0, itemSelectedIndex: 0),
            Basket(elementID: 0, image: "Tee", title: "Linear Near Tee", brand: "Converse", price: "$28.50", selectionImage: "checkmark", discountedPrice: "$19.99", sizeSelectedIndex: 0, colorSelectedIndex: 0, itemSelectedIndex: 0)
        ]
}
struct BasketList {
    static let listData: [Basket] = [
            Basket(elementID: 1, image: "Tee", title: "3-Stripes Shirt", brand: "Adidas Original", price: "$79.40", selectionImage: "checkmark", discountedPrice: "$48.99", sizeSelectedIndex: 0, colorSelectedIndex: 0, itemSelectedIndex: 0),
            Basket(elementID: 2, image: "Blouse", title: "Tuxedo Blouse", brand: "Lost Ink", price: "$50.00", selectionImage: "checkmark", discountedPrice: "$34.90", sizeSelectedIndex: 0, colorSelectedIndex: 0, itemSelectedIndex: 0),
            Basket(elementID: 3, image: "Tee", title: "Linear Near Tee", brand: "Converse", price: "$28.50", selectionImage: "checkmark", discountedPrice: "$19.99", sizeSelectedIndex: 0, colorSelectedIndex: 0, itemSelectedIndex: 0)
        ]
}
修复-:

struct BasketList {
    static let listData: [Basket] = [
            Basket(elementID: 0, image: "Tee", title: "3-Stripes Shirt", brand: "Adidas Original", price: "$79.40", selectionImage: "checkmark", discountedPrice: "$48.99", sizeSelectedIndex: 0, colorSelectedIndex: 0, itemSelectedIndex: 0),
            Basket(elementID: 0, image: "Blouse", title: "Tuxedo Blouse", brand: "Lost Ink", price: "$50.00", selectionImage: "checkmark", discountedPrice: "$34.90", sizeSelectedIndex: 0, colorSelectedIndex: 0, itemSelectedIndex: 0),
            Basket(elementID: 0, image: "Tee", title: "Linear Near Tee", brand: "Converse", price: "$28.50", selectionImage: "checkmark", discountedPrice: "$19.99", sizeSelectedIndex: 0, colorSelectedIndex: 0, itemSelectedIndex: 0)
        ]
}
struct BasketList {
    static let listData: [Basket] = [
            Basket(elementID: 1, image: "Tee", title: "3-Stripes Shirt", brand: "Adidas Original", price: "$79.40", selectionImage: "checkmark", discountedPrice: "$48.99", sizeSelectedIndex: 0, colorSelectedIndex: 0, itemSelectedIndex: 0),
            Basket(elementID: 2, image: "Blouse", title: "Tuxedo Blouse", brand: "Lost Ink", price: "$50.00", selectionImage: "checkmark", discountedPrice: "$34.90", sizeSelectedIndex: 0, colorSelectedIndex: 0, itemSelectedIndex: 0),
            Basket(elementID: 3, image: "Tee", title: "Linear Near Tee", brand: "Converse", price: "$28.50", selectionImage: "checkmark", discountedPrice: "$19.99", sizeSelectedIndex: 0, colorSelectedIndex: 0, itemSelectedIndex: 0)
        ]
}

您的elementID不是篮子对象的唯一标识符。我想给您打分。你能给我回信吗。这解决了我的问题。我不敢相信我错过了。当然,让我发布一个答案。