SwiftUI Grid View

How do you select items in a grid view ? There appears to be no equivalent to List(selection:)

Replies

Grids are just a way to organize Views, very much like Stack, without all the special powers of List inherited from UITableView. You have to manually handle the selection and here's a simple approach:

Code Block swift
struct ContentView: View {
    @State var selectedItem = 0
    var body: some View {
        ScrollView {
            LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: 3)) {
                ForEach((0...100), id: \.self) { item in
                    Rectangle()
                        .fill(item == selectedItem ? Color.blue : Color.gray)
                        .aspectRatio(1.0, contentMode: .fit)
                        .onTapGesture {
                            selectedItem = item
                        }
                }
            }
        }
    }
}