ios - 如何在 SwiftUI 的列表中设置和使用参数 "selection"

标签 ios swift list swiftui swift5

我了解了 SwiftUI,但在理解 SwiftUI 中的 List 时遇到困难。

列表定义如下。

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

    :
    :

}

那么我的问题是,如何才能拥有支持多选/单选的List呢? 我会知道如何设置 Binding<Set<SelectionValue>>? 的参数和 Binding<Set<SelectionValue>>? .

我已阅读 How does one enable selections in SwiftUI's List已经,我有这个代码。此代码支持多选。

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)"))
        }
    }
}

但仍然无法理解如何设置参数“selection”并设置类型。如何更改为单选列表?什么是 Set<String>() ...?

有没有人解释简单易懂? 我会有简单的例子...

非常感谢老师!感谢您阅读我的问题!!

最佳答案

How can I change to single selection List?

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

What is Set()...?

所选项目的容器,在您的例子中是 demoData

中的字符串

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

一种变体在 .onAppear 中,如下所示

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

通过状态变量的类型检测选中的类型,如果设置为多选,如果为可选则为单选。

关于ios - 如何在 SwiftUI 的列表中设置和使用参数 "selection",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61430852/

相关文章:

ios - 自动布局和 sizeForChildContentContainer

ios - 获取 SCNText Geometry 的宽度/高度

ios - UnhandledPromiseRejectionWarning:找不到xcodebuild

Swift:如何使用 Xcoordinator 从子协调器设置 Root View Controller ?

python - python 分割数组

jquery - 使用 Jquery UI sortable 一次对 2 个列表进行排序

ios - 如何使用 objective-c 格式化从 youtube api v3 reuqest 返回的持续时间?

ios - 如何在 swift 中用 nsattribute 替换 html 标签?

ios - 在 Swift 中为 CAGradientLayer 设置动画

java - ArrayList 自动添加空项