
WeChat0110a57982bedcf986616fadfb95cd1d.png
///需要實(shí)現(xiàn) Identifiable 協(xié)議
struct TodoTtem: Identifiable {
var id: UUID = UUID()
var task: String
var imgName: String
}
struct ContentView: View {
//listData 添加 @State 裝飾,這樣數(shù)據(jù)變化,會(huì)驅(qū)動(dòng)視圖更新
@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 {
///簡(jiǎn)單列表
// List {
// Text("第1頁")
// Text("第2頁")
// Text("第3頁")
// Text("第4頁")
// }
///動(dòng)態(tài)列表
List {
//加入分區(qū)
Section(header: Text("待辦事項(xiàng)")) {
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())
}
///移動(dòng)
func moveItem(from source: IndexSet, to destination: Int) {
listData.move(fromOffsets: source, toOffset: destination)
}
///刪除
func deleteItem(at offsets: IndexSet) {
listData.remove(atOffsets: offsets)
}
}
如果本文對(duì)你有幫助,歡迎點(diǎn)贊、評(píng)論、關(guān)注…