0

我有两个 DisclosureGroup,每个都有一个选择器。第一个选择器只是从一个静态的年份数组中读取,并且可以正常工作。第二个选择器填充用户名的选择器,但当您选择用户时,该值是 Firebase 文档 ID,而不是全名值。

// MARK: - DISCLOSER GROUP TRIP YEAR
   DisclosureGroup("Trip Year: \(String(selectedYear))", isExpanded: $tripYearIsExpanded) {
      Picker(selection: $selectedYear, label: Text("")) {
         ForEach(years, id: \.self) {
            Text(String($0))
         } // END:FOREACH
      } // END:PICKER
     .onChange(of: selectedYear, perform: { value in
        getTrips(passedYear: selectedYear)
      })
    } // END:DISCLOSUREGROUP
     .padding(.horizontal)
                
    // MARK: - DISCLOSURE GROUP DRIVER
    DisclosureGroup("Driver: \(selectedDriver)", isExpanded: $driverIsExpanded) {
      Picker(selection: $selectedDriver, label: Text("")) {
         ForEach(userListVM.userVM, id: \.id) { user in
            Text(user.fullname)
         } // END:FOREACH
      } // END:PICKER
        .onChange(of: selectedDriver, perform: { value in
           print("SELECTED DRIVER: \(selectedDriver)")
        })
      }
      .padding(.horizontal)
4

1 回答 1

1

如果你真的想selectedDriver成为这个名字,你可以改变你ForEach的基础fullname而不是id

ForEach(userListVM.userVM, id: \.fullname) {

然而,如果你曾经有过两个司机同名的情况,这会产生有趣的结果。

也许更好的选择是从选定的 ID 导出模型:

onChange

print("SELECTED DRIVER ID: \(selectedDriver)")
let selectedDriverModel = userListVM.userVM.first { $0.id == selectedDriver }
print("Selected driver name: \(selectedDriverModel?.fullname ?? "")")
于 2021-02-27T22:43:05.563 回答