List 是 SwiftUI 中用于显示动态列表的容器视图。它接受一个或多个子视图,并根据提供的数据动态生成对应的列表项。

下面是 List 的常见用法和一些常用的参数:

基本用法: List {

Text("Item 1")

Text("Item 2")

Text("Item 3")

} 在这个例子中,List 包含了三个 Text 视图作为列表项。

使用 ForEach 进行动态列表生成:

let items = ["Item 1", "Item 2", "Item 3"]

List {

ForEach(items, id: \.self) { item in

Text(item)

}

}

 

在这个例子中,使用 ForEach 循环遍历 items 数组生成相应的列表项。

自定义列表项样式: struct ItemRow: View {

var item: String

var body: some View {

HStack {

Image(systemName: "circle")

Text(item)

}

}

相关文章

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: