xcode - SwiftUI 3 MacOs 表格单选并双击打开表格

标签 xcode macos swiftui xcode13

import SwiftUI

struct ContentView: View {
    
    @State private var items: [ItemModel] = Array(0...100).map { ItemModel(id: $0, title: "item \($0)", age: $0) }
    @State private var selection = Set<ItemModel.ID>()
    @State private var sorting = [KeyPathComparator(\ItemModel.age)]
    
    var body: some View {
        Table(items, selection: $selection, sortOrder: $sorting) {
            TableColumn("id", value: \.id) { Text("\($0.id)") }
            TableColumn("title", value: \.title)
            TableColumn("age", value: \.age) { Text("\($0.age)") }
        }
        .onChange(of: sorting) {
            items.sort(using: $0)
        }
        .font(.caption)
        .frame(width: 960, height: 540)
    }
}

struct ItemModel: Identifiable {
    var id: Int
    var title: String
    var age: Int
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
这是在 Model.age 上排序的表的工作示例,并支持多选,
我想要单选并双击一行打开工作表,这可能吗?
另外我如何获得选定的项目对象?
谢谢🙏

最佳答案

必须改Set<Value.ID>Value.ID仅选择一行,并制作 TapGesture在文本中。

@State private var selection = Set<ItemModel.ID>() // <-- Use this for multiple rows selections
@State private var selection : ItemModel.ID? // <--- Use this for only one row selection
struct ContentView: View {
    
    @State private var items: [ItemModel] = Array(0...100).map { ItemModel(id: $0, title: "item \($0)", age: $0) }
    //@State private var selection = Set<ItemModel.ID>() <-- Use this for multiple rows selections
    @State private var selection : ItemModel.ID? // <--- Use this for only one row selection
    @State private var sorting = [KeyPathComparator(\ItemModel.age)]
    @State private var showRow = false
    
    var editRow: some View {
        VStack {
            Text(items[selection!].title)
                .font(.title)
            Text("Selected: \(selection.debugDescription)")
             Button("Dismiss") {
                showRow.toggle()
             }.padding()
        }
        .frame(minWidth:400, minHeight: 400)
    }
    
    var body: some View {
        VStack {
            Table(items, selection: $selection, sortOrder: $sorting) {
                TableColumn("id", value: \.id) {
                    Text("\($0.id)")
                        .onTapGesture(count: 2, perform: {
                            if selection != nil {
                                showRow.toggle()
                            }
                        })
                }
                TableColumn("title") { itemModel in
                    Text(itemModel.title)
                        .onTapGesture(count: 2, perform: {
                            if selection != nil {
                                showRow.toggle()
                            }
                        })
                }
                TableColumn("age", value: \.age) { Text("\($0.age)") }
            }
            .onChange(of: sorting) {
                items.sort(using: $0)
            }
            .font(.caption)
            .frame(width: 960, height: 540)
        }
        .sheet(isPresented: $showRow) {
            editRow
        }
    }
}

关于xcode - SwiftUI 3 MacOs 表格单选并双击打开表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67978340/

相关文章:

objective-c - NSURL 不带 %

ios - SwiftUI 中的淡入淡出图像似乎不起作用

ios - 我使用 objective-c 在 xcode 上编写了一个程序。我可以在 Windows 机器上运行它吗?

objective-c - 用于假人的Objective-C:如何在NSDictionary内部循环浏览NSDictionary?

macos - cmake find_package 在 Mac OS X 上不工作

java - Java Eclipse SDK无法构建项目,我也不知道为什么

SwiftUI 动画列表行的展开和折叠

swiftui - 如何在 SwiftData 和 SwiftUI 中使用 Swift 宏从数组中过滤对象

ios - 无法使用 Xcode 7.1 上传到 App Store

不同语言的 iOS UI 自动化