SwiftUI / SwiftData / 自定义选择器选择状态在新选择时不会改变

问题描述 投票:0回答:1

我在此处的帮助下有一个自定义选择器,因为如果我使用普通选择器,我的图像会渲染得很大。

问题是,我无法让它以编程方式工作。它曾经有效,直到我插入 来自我的模型的数据。

我已经尝试过打开包装、使用标签和使用 id,但没有任何效果。

我知道,图像现在不会改变,但如果我让选择器以某种方式改变值,我会尝试弄清楚这一点。

下面的代码,以及所属部分:

@Query private var bonsai: [BonsaiModel]
@State private var selectedBonsai: BonsaiModel?

Section("Select Bonsai:") {
                    LabeledContent("Bonsai") {
                        HStack {
                            Image("d2")
                                .renderingMode(Image.TemplateRenderingMode.original)
                                .resizable()
                                .scaledToFit()
                                .frame(maxWidth: 60, maxHeight: 35)
                                .clipShape(RoundedRectangle(cornerRadius: 3))
                            Menu {
                                // put the picker inside a menu, so users see the images in the menu
                                Picker(selection: $selectedBonsai) {
                                    ForEach(bonsai) { item in
                                        Label {
                                            if let spec = item.species {
                                                Text(spec.name)
                                                    .foregroundColor(Color("plainTextColor"))
                                                    .font(.footnote)
                                                    .fontWeight(.semibold)
                                                    
                                            } else {
                                                Text("No species selected.")
                                                    .foregroundColor(Color("plainTextColor"))
                                                    .font(.footnote)
                                                    .fontWeight(.semibold)
                                                    
                                            }
                                        } icon: {
                                            if let imageData = item.bonsaiImageData, // unwrapp data from item.image and store in imageData
                                               let uiImage = UIImage(data: imageData) { // Put imgageData in UIImage and store image in uiImage
                                                Image(uiImage: uiImage)
                                            } else {
                                                Image(systemName: "tree")
                                            }
                                        }.id(item)
                                    }
                                    
                                    
                                } label: { }
                            } label: {
                                HStack {
                                    if let species = selectedBonsai {
                                        Text(species.name)
                                            .foregroundColor(Color("plainTextColor"))
                                            .font(.footnote)
                                            .fontWeight(.semibold)
                                    } else {
                                        Text("No species selected.")
                                            .foregroundColor(Color("plainTextColor"))
                                            .font(.footnote)
                                            .fontWeight(.semibold)
                                    }
                                    Image(systemName: "chevron.up.chevron.down")
                                }
                                .tint(.secondary)
                                .imageScale(.small)
                            }
                        }
                    }
                }

选择器看起来像这样:

谢谢你:)

swift swiftui picker
1个回答
0
投票

尝试使用

.tag(item as BonsaiModel?)
而不是
.id(item)

© www.soinside.com 2019 - 2024. All rights reserved.