SwiftUI 学习笔记

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

2020-08-10  本文已影响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
上一篇 下一篇

猜你喜欢

热点阅读