美文网首页SwiftUI 学习笔记
用 List、ForEach 和 Section 创建简单的列表

用 List、ForEach 和 Section 创建简单的列表

作者: 艾迪不是奥特曼 | 来源:发表于2020-08-10 11:05 被阅读0次
image.png

整个 List 有分成若干个 Section,每个 Section 有标题 header

struct ListAndForEach: View {
    var body: some View {
        List {
            Section (header: Text("aaa")) {
                Text("Hello, World!")
            }
            
            Section (header: Text("bbb")) {
                ForEach(1..<10) {
                    Text("第 \($0) 个")   //用 $0 简写参数名
                }
            }
            
            Section (header: Text("ccc")) {
                Text("Hello, World!")
                Text("Hello, World!")
            } 
        }
         .listStyle(GroupedListStyle())  //添加列表的样式
    }
}

如果 ForEach 遍历的对象是一个动态的数组,那可以用以下方式:

image.png

相关文章

网友评论

    本文标题:用 List、ForEach 和 Section 创建简单的列表

    本文链接:https://www.haomeiwen.com/subject/oobddktx.html