我正在迅速申请,并试图选择不同河流的菜单。 目前,我面临的问题是,在开标后首次选择一条不同的河流时,它获得了选取选择的缺省指数(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
}
}
`