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!!