[英]SwiftUI: edit List inside of a Section
Without Form
and Section
, I can edit a list:没有Form
和Section
,我可以编辑一个列表:
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:我想在Form
的Section
内进行编辑,但它在那里不起作用:
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.