【问题标题】:Cell PickerTextField is not tappableCell PickerTextField 不可点击
【发布时间】:2021-02-22 06:49:03
【问题描述】:

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

当我检查 BasketListCell 预览时,它就像我想要的那样。所有的选择器都可以很好地使用占位符。但在滚动视图中却没有。

我在 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?

}

这是我的演示 BasketList,包含 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)
        ]
}

那是我的 BasketCell

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)
        }
       }

最后,这是我的 ShoppingCartView

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 预览

那是我的 ShoppingCartView

【问题讨论】:

  • 您的 elementID 不是 Basket 对象的唯一标识符。
  • 我要给你积分。你能把它作为答案发送吗?这解决了我的问题 Tushar。我不敢相信我错过了。
  • 当然让我发布答案。

标签: swiftui uiscrollview picker


【解决方案1】:

您的模型类Basket 的elementID 属性必须是唯一的。您当前有 3 个Basket 对象,所有对象都带有重复的identifier,导致 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)
        ]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-20
    • 2012-05-10
    相关资源
    最近更新 更多