SwiftUI editable List in Form in separate View (like Picker)

Viewed 1008

I'd like to access an editable List from a Form similar to a Picker, where only one entry is shown in the Form and if you click on the entry, you get to see all available options.

If I use List in a Form, it behaves differently. How can I modify my code?

struct ContentView: View {
    @State var listItems = ["1", "2", "3"]
    @State var pickerIndex = 0
    
    var body: some View {
        
        NavigationView {
            
            Form{
                Section{
                    
                    //this is how I want it to be: one entry in the Form
                    Picker(selection: self.$pickerIndex, label: Text("Picker")){
                        Text("1").tag(0)
                        Text("2").tag(1)
                        Text("3").tag(2)
                    }
                    
                    //this is not how I like it
                    List {
                        ForEach(listItems, id: \.self) { (item) in
                            Text(item)
                        }.onMove { (indexSet, index) in
                            self.listItems.move(fromOffsets: indexSet,
                                                toOffset: index)
                        }
                    }
                }
            }
                
            .navigationBarTitle(Text("Example"))
            .navigationBarItems(trailing: EditButton())
        }
    }
}

enter image description here

0 Answers
Related