Thuta Learning
ရှာဖွေရန်
IntermediateMobile Developmentintermediate

Lists (List, ForEach)

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Lists (List, ForEach) ကို ကြောက်စရာမလိုအောင် နားလည်မယ်
  • ကိုယ်တိုင် Xcode/SwiftUI code ကို run ကြည့်တတ်မယ်
  • Real project ထဲမှာ ဒီ concept ကို ချက်ချင်း အသုံးချတတ်မယ်

ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်

`VStack` ထဲမှာ `ForEach` သုံးရင် child view အားလုံးကို တစ်ပြိုင်နက် render လုပ်ပါတယ် — item ၁၀၀၀ ရှိရင် performance ကျဆင်းနိုင်ပါတယ် (scroll လုပ်ဖို့ `ScrollView` ကို manual ထပ်ထားရပါ)။ `List` ကတော့ built-in scrollable container ပါ — visible item ကိုသာ efficiently render (lazy loading, Android ရဲ့ `LazyColumn`/RecyclerView နဲ့ ဆင်တူပါတယ်), swipe-to-delete/pull-to-refresh စတဲ့ native iOS behavior ကိုပါ built-in ပေးပါတယ်.

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

Todo list app မှာ `List(todos) { todo in TodoRow(todo: todo) }` လို့ ရေးရင် — todo item ၁၀၀ ရှိပေမယ့် screen ပေါ်မှာ visible ဖြစ်နေတဲ့ item များကိုပဲ render လုပ်ပြီး, native iOS scroll/swipe behavior (swipe-to-delete) ကိုပါ အလိုအလျောက် ရရှိပါတယ် — `todos` ရဲ့ item တစ်ခုချင်းစီက `Identifiable` protocol ကို conform လုပ်ထားဖို့ လိုပါတယ် (TodoItem ရဲ့ `id` property ကနေ).

အတူတူ ကြည့်မယ်

swift
struct TodoListScreen: View {
    let todos: [TodoItem]

    var body: some View {
        List(todos) { todo in
            HStack {
                Image(systemName: todo.isDone ? "checkmark.circle.fill" : "circle")
                Text(todo.title)
            }
        }
    }
}
You should see
Todo item ၅-၁၀ ခုကို scroll လုပ်ရင် native iOS style (swipe-to-delete ပါ) ဖြင့် ကြည့်ရှုနိုင်တဲ့ list ကို Simulator ပေါ်မှာ တွေ့ရမည်။

၅ မိနစ် စမ်းကြည့်

`TodoItem` (Basic chapter struct) ရဲ့ array (item ၁၀ ခု sample data) ကို create ပြီး `TodoListScreen` ကို run ကြည့်ပါ — scroll လုပ်ကြည့်ပြီး smooth ဖြစ်တာ confirm လုပ်ကြည့်ပါ။

သတိလေးတစ်ချက်

`List` ရဲ့ default styling (row separator, background) ကို remove/customize ချင်ရင် `.listStyle()` modifier ကို သုံးရပါမယ် — default appearance က design mockup နဲ့ မကိုက်ညီနိုင်ပါ။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • TodoItem ကို `Identifiable` protocol conform မလုပ်ဘဲ `List` ထဲ ထည့်ကြည့်ခြင်း — compile error ဒါမှမဟုတ် `id:` parameter manual ထည့်ရန် လိုအပ်ပါလိမ့်မယ်
  • Item ၅-၆ ခုပဲ ရှိတဲ့ short list မှာတောင် `List` default သုံးနေခြင်း — item နည်းရင် `VStack` က ရိုးရိုးရှင်းရှင်း ပိုသင့်တော်ပါတယ်

အခု ကိုယ်တိုင် စမ်းကြည့်

`TodoItem` (Basic chapter struct) ရဲ့ array (item ၁၀ ခု sample data) ကို create ပြီး `TodoListScreen` ကို run ကြည့်ပါ — scroll လုပ်ကြည့်ပြီး smooth ဖြစ်တာ confirm လုပ်ကြည့်ပါ။

You'll know it worked when: Todo item ၅-၁၀ ခုကို scroll လုပ်ရင် native iOS style (swipe-to-delete ပါ) ဖြင့် ကြည့်ရှုနိုင်တဲ့ list ကို Simulator ပေါ်မှာ တွေ့ရမည်။

Lists (List, ForEach) | Thuta Learning