【问题标题】:How to select item in List view如何在列表视图中选择项目
【发布时间】:2020-12-21 13:08:35
【问题描述】:

我在 SwiftUI 中有以下代码

struct AuthorView: View {

let authors = ["a","b","c"]
@State private var selectedAuthor = ""

var body: some View {
    VStack {
        Text("Authors").font(.title)
        HStack {
            List(authors, id: \.self, selection: $selectedAuthor) {author in
                Text(author).tag(author)
            }
            Spacer()
        }
    }
}

但由于“无法推断通用参数'SelectionValue'”错误,我无法编译。我在这里做错了什么?我的目标是让选定的作者供以后使用。 谢谢。

【问题讨论】:

    标签: list swiftui


    【解决方案1】:

    List接口契约选择应该是可选的,所以这里是固定部分

    struct AuthorView: View {
    
       let authors = ["a","b","c"]
       @State private var selectedAuthor: String?      // << !!
    
    // ... other code
    }
    

    【讨论】:

      猜你喜欢
      • 2012-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多