SwiftUI基礎(chǔ)之List用法詳解

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)注…

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容