Latest Post

How to use closure button in swift How to use notificationcenter in swiftui with example Longest common subsequence program in swift
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>()

 

Leave a Reply

Your email address will not be published.