Latest Post

flutter interview questions and answers What is NSURLSession & URL Session and Networking? How to use Swiftui searchable
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.

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>()