0

Without Form and Section, I can edit a list:

var body: some View {
            List {
                ForEach(modeConfigurations.sorted, id: \.id) { item in
                    Text("\(item.defaultSortIndex)")
                }
                .onMove(perform: move)
            }
            .navigationBarItems(trailing:
                   EditButton()
            )
} // Body

enter image description here

I'd like to edit inside a Section of a Form, but it doesn't work there:

var body: some View {
    Form{
        Section(header: Text("Sort")){
            List {
                ForEach(modeConfigurations.sorted, id: \.id) { item in
                    Text("\(item.defaultSortIndex)")
                }
                .onMove(perform: move)
            }
            .navigationBarItems(trailing:
                   EditButton()
            )
        } // Sort Section
    } // Form
} // Body

I can't edit and the Text inside ForEach is not rendered as separate line.

enter image description here

How can I edit a List inside a Section of a Form?

1 Answer 1

2

You should put .navigationBarItems(trailing: EditButton()) on the Form instead to make it work.

Also List is not needed as Section already "acts like" a List. (Thanks to @Sweeper for mentioning that)

var body: some View {
    Form {
        Section(header: Text("Sort")) {
            // List { // <- This is not needed as Section contains an implicit list.
                ForEach(modeConfigurations.sorted, id: \.id) { item in
                    Text("\(item.defaultSortIndex)")
                }
                .onMove(perform: move)
            // } // <- Removeed this as we removed `List`
        } // Sort Section
    } // Form
    .navigationBarItems(trailing: EditButton()) // <- Misplacing this was the issue.
} // Body
Sign up to request clarification or add additional context in comments.

2 Comments

I don't think you need the List either. The Section already "acts like" a List.
I was aware to change the original code. But as you mentioned, I decided to add it to the answer. Thanks @Sweeper

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.