为什么我新选择的选择器值没有显示在视图中?

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

我有一系列高尔夫球开球组,我将它们呈现在列表中,并使用选择器为每个高尔夫球组对象选择起始洞。

以下示例应该是可重现的。我希望当我从下拉列表中选择一个值时,我会看到新选择的值。我没有 - 它仍然显示初始值。

// separate structs



struct FPStartingHole: Identifiable {
    let id: UUID
    let hole: String
}


struct simpleTeeTime: Codable, Hashable, Identifiable {
  
   varid:                  UUID
   var startingHole:        String     // note:  it could be 1, 1A, or 1B... 18B

    
   enum CodingKeys: String, CodingKey {
        case id, startingHole
   }
 
    init(
        id:                   UUID      = UUID(),
        startingHole:         String    = "1A"
        
    ) {
        self.id                  = id
        self.startingHole        = startingHole
    }
    
}





          

//inside the main view struct

    @State private var masterTeeTimes: [simpleTeeTime] = [
        
        simpleTeeTime(id: UUID(), startingHole: "7"),
        simpleTeeTime(id: UUID(), startingHole: "7A"),
        simpleTeeTime(id: UUID(), startingHole: "8"),
        simpleTeeTime(id: UUID(), startingHole: "8A")

    ]
    

let fpstartingHoles: [FPStartingHole] = [
    FPStartingHole(id: UUID(), hole: "7" ),
    FPStartingHole(id: UUID(), hole: "8A"),
    FPStartingHole(id: UUID(), hole: "9B")
]

    







        Form {
        
           Section() {
                    
                    VStack{
                            // for each golf tee time, use a picker to select the starting hole for each golf group.
                        ForEach(0..<3) { i in

                            HStack(alignment: .firstTextBaseline){
                                
                                
                                Spacer()
                                
                                
                                Picker( "Start Hole", selection: $masterTeeTimes[i].startingHole)  {
                                    
                                    ForEach( fpstartingHoles, id: \.id) { shole in
                                        Text(shole.hole).tag(shole.id)
                                    }
                                    
                                }
                                
                                .frame(width: 30)
                                .pickerStyle(DefaultPickerStyle())
                                .padding(EdgeInsets(top: 5, leading: 0, bottom: 5, trailing: 12))
                                
                            }
                            .frame(width: screenSize.maxX- 50, height: 70, alignment: .leading)
                            .background(Color.blue.opacity(0.1))
                            .cornerRadius(10)
                            .overlay(RoundedRectangle(cornerRadius: 10)
                                .stroke(Color.orange, lineWidth: 3))
                            .shadow(radius: 10)
                            
                        }
                    }
                }
             }

非常感谢您的帮助。

swiftui foreach swift5 picker
© www.soinside.com 2019 - 2024. All rights reserved.