SwiftUI查漏补缺(3) - SwiftUI中如何给List添加Header和Footer

3周前 65次点击 来自 SwiftUI

收录专题: SwiftUI查漏补缺

原文链接:
SwiftUI Header and Footer List Tutorial

这类问题都是固定写法,没有太大的技术难度,容易忘记,所以在此做一个备忘:

struct ContentView: View {
    // 1.
    let europeanCars = ["Audi","Renault","Ferrari"]
    let asianCars = ["Honda","Nissan","Suzuki"]
    
    var body: some View {
        NavigationView {
            List {
                // 2.
                Section(header:
                    Text("European Cars")) {
                        ForEach(0 ..< europeanCars.count) {
                            Text(self.europeanCars[$0])
                        }
                    }
                // 3.
                Section(header:
                    HStack {
                        Image(systemName: "car")
                        Text("Asian Cars")
                    }
                // 4.
                , footer: Text("This is a example list of a few car brands").font(.footnote))  {
                               ForEach(0 ..< asianCars.count) {
                                   Text(self.asianCars[$0])
                               }
                           }
            
            } .navigationBarTitle("Cars")
        }
           
    }
}

代码一目了然,效果如下:
给List添加Header和Footer

Card image cap
开发者雷

尘世间一个小小的开发者

技术文档 >> 系列应用 >>
热推应用

EntryS

学习Swift的入门教程
标签