2

我正在尝试实现具有多项选择的列表。

这是代码:

import SwiftUI

struct MultiSelectionWithEditButton : View {
    var items = ["One","Two", "Three"]

    @State var selectedRows = Set<String>()

    var body: some View {
        NavigationView {
            List((items, id: \.self), selection: $selectedRows) { name in
                Text(name)
                    .font(.title)
                    .fontWeight(.bold)
                }
                .navigationBarItems(trailing: EditButton())
                .navigationBarTitle(Text("Selected \(selectedRows.count) rows"))
        }
    }
}

#if DEBUG
struct MultiSelectionWithEditButton_Previews : PreviewProvider {
    static var previews: some View {
        MultiSelectionWithEditButton()
    }
}
#endif

但我得到以下错误:

“没有更多上下文,表达的类型是模棱两可的”

在此处输入图像描述

感谢是否有人可以提供帮助!:)

4

1 回答 1

2

问题出在你List的初始化中。将其更改为Listwith ForEachinside,如下所示:

struct MultiSelectionWithEditButton : View {
    var items = ["One","Two", "Three"]

    @State var selectedRows = Set<String>()

    var body: some View {
        NavigationView {
            List(selection: $selectedRows){
                ForEach(items, id: \.self) { name in
                    Text(name)
                        .font(.title)
                        .fontWeight(.bold)
                }
            }
            .navigationBarItems(trailing: EditButton())
            .navigationBarTitle(Text("Selected \(selectedRows.count) rows"))
        }
    }
}
于 2019-09-23T13:20:51.997 回答