要将 Section 添加到 SwiftUI 列表视图中,可以使用 List 控件的 init 方法,其支持将一个数组作为其第一个参数,该数组中的每个元素都可以是承载着一个或多个 row 的 Section。
例如:
struct ContentView: View {
var body: some View {
List {
Section(header: Text("Section 1")) {
Text("Row 1")
Text("Row 2")
}
Section(header: Text("Section 2")) {
Text("Row 3")
Text("Row 4")
}
}
}
}
在这个例子中,我们在 List 内部定义了两个 Section,它们分别包含两个 Text 行。 Section 通过 header 参数在界面上添加区域标题。
如果需要进行更具体的自定义,则可以通过修改 Section 和 List 控件的样式来实现更复杂的布局和效果。