English 中文(简体)
Picker 选择不匹配与代编码的Json河数据
原标题:Picker selection mismatch with decoded Json river data

我正在迅速申请,并试图选择不同河流的菜单。 目前,我面临的问题是,在开标后首次选择一条不同的河流时,它获得了选取选择的缺省指数(0)的河流数据。

例如,当我开口时,“多谷”是缺水的,它发现河流高度是正确的。 但 如果我选择另一种选择,它就使用“212021”而不是选择的“多谷”。

 let rivers: [River] = [
        River(id: "212021", name: "McDonald River @ Howes Valley"),
        River(id: "212461", name: "Hawkesbury River @ Leetsvale"),
        River(id: "212290", name: "Colo River @ Upper Colo")
    ]

**I have tried to modify my menu picker logic to select the index of the menu picker, but still having the issue. Here is the code for my River Data menu picker **

GroupBox(label: Label("River Data", systemImage: "waveform.path.ecg")
                .font(.title)
                .foregroundColor(.blue)
                .padding(.bottom, 8)
            ) {
                
                Picker(selection: $selectedRiverIndex, label: Text("Select a river")) {
                    ForEach(0..<rivers.count, id: .self) { index in
                        Text(rivers[index].name)
                    }
                }
                .pickerStyle(MenuPickerStyle())
                .onChange(of: selectedRiverIndex) { newIndex, _ in
                    isLoading = true // Show loading indicator
                    Task {
                        let newRiverIndex = newIndex
                        await riverModel.fetchRiverHeightData(for: rivers[newRiverIndex].id)
                        isLoading = false // Hide loading indicator after data is fetched
                    }
                }
              
              // River data details view
                VStack(alignment: .leading, spacing: 8) {
                    ForEach(riverModel.riverData, id: .time) { riverValue in
                        VStack(alignment: .leading) {
                            if let convertedDate = convertDateString(riverValue.time) {
                                Text("(riverValue.v)m")
                                    .font(.headline)
                                Text("Last Updated:")
                                    .font(.subheadline)
                                    .foregroundColor(.secondary)
                                Text("(convertedDate.date) at (convertedDate.time)")
                                    .font(.subheadline)
                            }
                        }
                        .padding(.vertical, 4)
                    }
                }
            }`

**
Here is the code for default when opening the app:**
 // Fetch river details and river data when the view appears
`        
.onAppear {
            Task {
                let selectedRiverID = "212021" // Set the initial selected river ID
                await riverModel.fetchRiverHeightData(for: selectedRiverID)
                isLoading = false // Hide loading indicator after data is fetched
            }
            
        }
`

问题回答

感谢@workingdog

由此可见,我需要在Change添加new Index

                .onChange(of: selectedRiverIndex) { oldIndex, newIndex in
                    isLoading = true
                    Task {
                        let newRiverIndex = newIndex
                        await riverModel.fetchRiverHeightData(for: rivers[newRiverIndex].id)
                        isLoading = false
                    }
                }




相关问题
Simple Q about UIPickerView properties

Ok this is probably so simple that it s laughable, but I can t figure the answer. What is the property of the uipickerview? For the UIDatePicker, its datePicker.date, for label you can do label.text, ...

UIPickerView with Multiline UILabel

I m currently working on a program that populates a picker view dynamically from my Core Data set up. I have everything working data-wise but the problem i m running into now is formatting on my ...

Is it possible to move a UIView and not its subviews?

I have a UIPickerView added as a subview to my main view. I also have a series of textfields in the main view, so the picker view covers some up. So I was wondering if it is possible to move the main ...

UIPicker didSelectRow Strange Behavior

I have a 3 component dependent picker and I had it working fine until I noticed a strange behavior. If I spin component 1 and then click down with mounse on Conmponent 2, then wait for Component 1 to ...

iPhone UIPickerView, insert a space between two components?

I have been building a UIPickerView with 2 components(wheels) and was wondering if there is a way to space them out a bit, horizontally? I haven t been able to find anything in either definition or ...

Not getting the selected value from a PickerView

Im doing an Iphone app with a UIPickerView. The picker view has two columns that contains NSStrings. I have a button that generates a NSString containing the selected value of both columns in the ...

UIPicker to open to a UIView

I have a UIPicker that is holding titles. I would like it to when a row is tapped to open to a .xib view/ UIView and have a small textview in it. Is this possable?? If so please help!! Thanks in ...

热门标签