【问题标题】:SwiftUI List how to identify what item is selected on macOSSwiftUI List 如何识别在 macOS 上选择了哪些项目
【发布时间】:2020-04-27 01:03:23
【问题描述】:

这是我基于this answer 的内容。该代码当前允许用户选择一个单元格,但我无法区分 哪个 单元格被选中或执行任何代码以响应选择。总之,如何根据所选单元格的名称执行代码并在单击时执行。该单元格当前在单击的位置以蓝色突出显示,但我想识别它并根据该选择采取相应的行动。注意:我不想在编辑模式下选择单元格。另外,如何在不点击的情况下以编程方式选择单元格?

struct OtherView: View {
    @State var list: [String]
    @State var selectKeeper = Set<String>()

    var body: some View {
        NavigationView {
            List(list, id: \.self, selection: $selectKeeper) { item in
                Text(item)
            }
        }
    }
}



这是一个演示选择的 gif

【问题讨论】:

    标签: macos swiftui swiftui-list


    【解决方案1】:

    我找到了一种解决方法,但必须单击文本本身——单击单元格没有任何作用:

    struct OtherView: View {
        @State var list: [String]
        @State var selectKeeper = Set<String>()
    
        var body: some View {
            NavigationView {
                List(list, id: \.self, selection: $selectKeeper) { item in
                    Text(item)
                      .onTapGesture {
                         print(item)
                      }
                }
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      列表选择在编辑模式下工作,所以这里是选择使用的一些演示

      struct OtherView: View {
          @State var list: [String] = ["Phil Swanson", "Karen Gibbons", "Grant Kilman", "Wanda Green"]
          @State var selectKeeper = Set<String>()
      
          var body: some View {
              NavigationView {
                  List(list, id: \.self, selection: $selectKeeper) { item in
                      if self.selectKeeper.contains(item) {
                          Text(item).bold()
                      } else {
                          Text(item)
                      }
                  }.navigationBarItems(trailing: HStack {
                      if self.selectKeeper.count != 0 {
                          Button("Send") {
                              print("Sending selected... \(self.selectKeeper)")
                          }
                      }
                      EditButton()
                  })
              }
          }
      }
      

      【讨论】:

      • 唯一的问题是 .navigationBarItems 在 macOS 上的 SwiftUI 上不起作用
      【解决方案3】:

      为了免除您的阵痛:

      import SwiftUI
      
      struct ContentView: View {
          @State private var selection: String?
          let list: [String] = ["First", "Second", "Third"]
          
          var body: some View {
              NavigationView {
                  HStack {
                      List(selection: $selection) {
                          ForEach(list, id: \.self) { item in
                              VStack {
                                  Text(item)
                              }
                          }
                      }
                      TextField("Option", text: Binding(self.$selection) ?? .constant(""))
                  }
                  .frame(minWidth: 100, maxWidth: .infinity, minHeight: 100, maxHeight: .infinity)
              }
          }
      }
      

      此解决方案处理 @Isaac 解决的问题。

      screenshot

      【讨论】:

        【解决方案4】:

        我的 2 美分用于自定义选择和操作 适用于 swift 5.1 / iOS14:

        见:

        https://gist.github.com/ingconti/49497419e5edd84a5f3be52397c76eb4

        我留下了很多调试“打印”......以便更好地理解。

        【讨论】:

          猜你喜欢
          • 2011-01-11
          • 1970-01-01
          • 2016-04-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-29
          • 1970-01-01
          相关资源
          最近更新 更多