Ios 如何设置和使用参数;选择“;在SwiftUI的列表中
我已经了解了SwiftUI,并且很难理解SwiftUI中的列表 列表定义如下Ios 如何设置和使用参数;选择“;在SwiftUI的列表中,ios,swift,list,swiftui,swift5,Ios,Swift,List,Swiftui,Swift5,我已经了解了SwiftUI,并且很难理解SwiftUI中的列表 列表定义如下 @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 multipl
@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)
:
:
}
@可用(iOS 13.0、OSX 10.15、tvOS 13.0、watchOS 6.0、*)
公共结构列表:视图,其中SelectionValue:Hashable,Content:View{
///创建支持多项选择的列表。
///
///-参数选择:与标识所选参数的集合的绑定
///排。
///
///-另请参见:`View.selectionValue`,它为行提供标识符。
///
///-注意:在iOS和tvOS上,您必须明确地将“列表”置于编辑状态
///要应用的选择的模式。
@可用(watchOS,不可用)
public init(选择:Binding?,@ViewBuilder content:()->content)
///创建支持可选单个选择的列表。
///
///-参数选择:绑定到可选的选定行。
///
///-另请参见:`View.selectionValue`,它为行提供标识符。
///
///-注意:在iOS和tvOS上,您必须明确地将“列表”置于编辑状态
///要应用的选择的模式。
@可用(watchOS,不可用)
public init(选择:Binding?,@ViewBuilder content:()->content)
:
:
}
那么我的问题是,我怎样才能拥有支持多个/单个选择的列表?
我知道如何设置Binding?
和Binding?
的参数
我已经读过了,我有这个代码。此代码不支持多个选择
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)"))
}
}
}
var demoData=[“Phil Swanson”、“Karen Gibbons”、“Grant Kilman”、“Wanda Green”]
结构ContentView:View{
@状态变量selectKeeper=Set()
var body:一些观点{
导航视图{
列表(解调数据,id:\.self,选择:$selectKeeper){name in
文本(名称)
}
.navigationBarItems(尾部:EditButton())
.navigationBarTitle(文本(“选择演示\(selectKeeper.count)”)
}
}
}
但仍然不明白如何设置参数“selection”和类型。如何更改为单一选择列表?什么是Set()
有人解释得容易理解吗?
我会有一个简单的例子
非常感谢你,老师!谢谢你阅读我的问题
如何更改为单一选择列表
选择的类型由状态变量的类型检测,如果设置为多选,则为可选,然后为单选。Asperi san,感谢您提供易于理解的答案!我很感激,老师:)我可以得到我想要的所有信息。
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)"))
}
}
}
List(demoData, id: \.self, selection: $selectKeeper){ name in
Text(name)
}
.onAppear {
self.selectKeeper = [demoData[0]]
}