SwiftUI: редактировать список внутри раздела - PullRequest
0 голосов
/ 21 июня 2020

Без Form и Section, я могу редактировать список:

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.

введите описание изображения здесь

Как мне отредактировать List внутри Section из Form?

1 Ответ

2 голосов
/ 21 июня 2020

Вы должны поставить .navigationBarItems(trailing: EditButton()) на Form вместо этого, чтобы он работал.

Также List не требуется, поскольку Section уже "действует как" a List. (Спасибо @Sweeper за это упоминание)

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
...