Jinsujin / SwiftUI

SwiftUI를 공부해보고 활용하는 공간
0 stars 0 forks source link

List #6

Closed Jinsujin closed 1 year ago

Jinsujin commented 1 year ago

문서 바로가기

Declaration

struct List<SelectionValue, Content> where Selection
Value : Hashable, Content : View

예제

List 에 Array 데이터 보여주기


struct Fruit: Identifiable {
    var id = UUID()
    let name: String
    let emoji: String
}

/// 데이터를 반복해서 보여줘야할때 사용
struct ListView: View {

    @State private var multiSelection = Set<UUID>()

    var body: some View {

        let fruits: [Fruit] = [
            Fruit(name: "Apple", emoji: "🍏"),
            Fruit(name: "Strawberry", emoji: "🍓"),
            Fruit(name: "Grape", emoji: "🍇"),
            Fruit(name: "Banana", emoji: "🍌"),
            Fruit(name: "Lemon", emoji: "🍋"),
            Fruit(name: "Cherry", emoji: "🍒")
        ]

        NavigationView {
            List(fruits, selection: $multiSelection) { fruit in
                HStack {
                    Text(fruit.emoji)
                        .bold()
                        .frame(width: 30, height: 80, alignment: .trailing)
                    Text(fruit.name)
                        .bold()
                }
            }
            .navigationTitle("Fruit shop")
            .toolbar { EditButton() }
        }
        Text("\(multiSelection.count) selections")
    }
}