简体   繁体   English

SwiftUI:编辑部分内的列表

[英]SwiftUI: edit List inside of a Section

Without Form and Section , I can edit a list:没有FormSection ,我可以编辑一个列表:

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

在此处输入图像描述

I'd like to edit inside a Section of a Form , but it doesn't work there:我想在FormSection内进行编辑,但它在那里不起作用:

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.我无法编辑,并且ForEach中的Text未呈现为单独的行。

在此处输入图像描述

How can I edit a List inside a Section of a Form ?如何编辑Form Section内的List

You should put .navigationBarItems(trailing: EditButton()) on the Form instead to make it work.您应该将.navigationBarItems(trailing: EditButton())放在Form上以使其工作。

Also List is not needed as Section already "acts like" a List .也不需要List ,因为Section已经“表现得像” List (Thanks to @Sweeper for mentioning that) (感谢@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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM