SwiftUI List的使用
程序员文章站
2022-06-01 11:03:50
...
演示创建一个三组,每组有四行数据的列表
import SwiftUI
struct ContentView: View {
@State var showRight:Bool
var body: some View {
NavigationView{
List {
//创建分组
ForEach (0 ..< 3) {(i) in
Section(header:Text("Section \(i)"), footer: Text("Fotter\(i)")){
//创建行内容
ForEach (0 ..< 4) {(i) in
return HStack {
Text("\(i)").listRowBackground(Color.red);
}
}
}
}
}
.onAppear {
//隐藏自带分割线
UITableView.appearance().separatorStyle = .none
}
.navigationBarTitle("Title")
.navigationBarItems(trailing: Button(showRight ? "Right" : "trailing"){
print("trailing button clicked!")
self.showRight.toggle()
})
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(showRight: false)
}
}
推荐阅读