///需要实现 Identifiable 协议
struct TodoTtem: Identifiable {
var id: UUID = UUID()
var task: String
var imgName: String
}
struct ContentView: View {
//listData 添加 @State 装饰,这样数据变化,会驱动视图更新
@State var listData: [TodoTtem] = [
TodoTtem(task: "微信", imgName: "icon_common_wechat"),
TodoTtem(task: "朋友圈", imgName: "icon_common_discover"),
TodoTtem(task: "i友圈", imgName: "icon_common_iyou"),
TodoTtem(task: "链接", imgName: "icon_common_copyLink")
]
var body: some View {
///简单列表
// List {
// Text("第1页")
// Text("第2页")
// Text("第3页")
// Text("第4页")
// }
///动态列表
List {
//加入分区
Section(header: Text("待办事项")) {
ForEach(listData) { item in
HStack {
Image(item.imgName)
.resizable()
.frame(width: 40, height: 40)
Text(item.task)
}
}
.onDelete(perform: deleteItem(at:))
.onMove(perform: moveItem(from:to:))
}
Section(header: Text("其他")) {
Text("Hello World")
}
}.listStyle(GroupedListStyle())
}
///移动
func moveItem(from source: IndexSet, to destination: Int) {
listData.move(fromOffsets: source, toOffset: destination)
}
///删除
func deleteItem(at offsets: IndexSet) {
listData.remove(atOffsets: offsets)
}
}
如果本文对你有帮助,欢迎点赞、评论、关注…
网友评论