3

I have learned about SwiftUI, and am having difficulties to understand List in SwiftUI.

The List definition is below.

@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public struct List<SelectionValue, Content> : View where SelectionValue : Hashable, Content : View {

    /// Creates a List that supports multiple selection.
    ///
    /// - Parameter selection: A binding to a set that identifies the selected
    ///   rows.
    ///
    /// - See Also: `View.selectionValue` which gives an identifier to the rows.
    ///
    /// - Note: On iOS and tvOS, you must explicitly put the `List` into Edit
    ///   Mode for the selection to apply.
    @available(watchOS, unavailable)
    public init(selection: Binding<Set<SelectionValue>>?, @ViewBuilder content: () -> Content)

    /// Creates a List that supports optional single selection.
    ///
    /// - Parameter selection: A binding to the optionally selected row.
    ///
    /// - See Also: `View.selectionValue` which gives an identifier to the rows.
    ///
    /// - Note: On iOS and tvOS, you must explicitly put the `List` into Edit
    ///   Mode for the selection to apply.
    @available(watchOS, unavailable)
    public init(selection: Binding<SelectionValue?>?, @ViewBuilder content: () -> Content)

    :
    :

}

Then my question is this, how can I have List that supports multiple/single selection? I would know how to set argument of Binding<Set<SelectionValue>>? and Binding<Set<SelectionValue>>?.

I have read How does one enable selections in SwiftUI's List already, and I've got this code. This code does support multiple selection.

var demoData = ["Phil Swanson", "Karen Gibbons", "Grant Kilman", "Wanda Green"]

struct ContentView: View {
    @State var selectKeeper = Set<String>()

    var body: some View {
        NavigationView {
            List(demoData, id: \.self, selection: $selectKeeper){ name in
                Text(name)
            }
            .navigationBarItems(trailing: EditButton())
            .navigationBarTitle(Text("Selection Demo \(selectKeeper.count)"))
        }
    }
}

But still can't understand how I can set the argument "selection" and set the type as well. How can I change to single selection List? What is Set<String>()...?

Does anyone explain easily to understand? I would have easy example...

Thank you so much, Sensei! Thank you for reading my question!!

Haru
  • 65
  • 1
  • 7

1 Answers1

5

How can I change to single selection List?

@State var selectKeeper: String? = nil // << default, no selection

What is Set()...?

A container for selected items, in your case strings from demoData

how I can set the argument "selection" and set the type as well

One variant is in .onAppear as below

List(demoData, id: \.self, selection: $selectKeeper){ name in
    Text(name)
}
.onAppear {
   self.selectKeeper = [demoData[0]]
}

Type of selected is detected by type of state variable, it if Set that it is multi-selection, if it is optional, then single selection.

Asperi
  • 228,894
  • 20
  • 464
  • 690
  • Asperi-san, thanks for giving your easy-understanding answer! I appreciate it Sensei:) I could get all of information i wanted. – Haru Apr 30 '20 at 01:39