Swiftui list Selection : Row Selection in SwiftUI

Sep 02, 2021 swiftui

Swiftui list Selection : Row Selection in SwiftUI

selection list of  Particular Row in SwiftUI. Swift Supports both single selection and as well as multiple Selection in List.Swiftui list Selection

Earlier we used to have tableView DidSelect method in Storyboard but now we have list we can add selection of row in Swift.

Table of Contents

Swiftui list Selection

To create a Single selection in SwiftUI we needed to declare the variable as vehicleSelected as shown in below code

struct ContentView: View {

    let vehicles = [

        "Honda",

        "BMW",

        "Tesla",

        "Ferrari"

    ]

    @State private var vehicleSelected: String?

    var body: some View {

        NavigationView {

            List(vehicles, id: .self, selection: $vehicleSelected) { vehicle in

                Text(vehicle)

            }

            .navigationTitle("Vehicle Selection")

            .toolbar {

                EditButton()

            }

        }

    }

}

we can select the multiple row by changing the type of vehicleSelected by using Set

@State private var selection = Set<String>()

 

Table of Contents

Index