vdo*_*tup 5 macos xcode 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()
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个在 Model.age 上排序的表格的工作示例,并且支持多项选择,我想要单选并双击一行打开工作表,这可能吗?另外我如何获取所选项目对象?
谢谢
关于双击表格行:Apple 在 WWDC 2022 上使用 SwiftUI 4 引入了新的上下文菜单修饰符contextMenu(forSelectionType:menu:primaryAction:)。这样,primaryAction可以提供当用户双击一行时执行的Table操作。
@State private var selection: ItemModel.ID?
var body: some View {
Table(items, selection: $selection, sortOrder: $sortOrder) {
TableColumn("id", value: \.id)
TableColumn("title", value: \.title)
TableColumn("age", value: \.age)
}
.contextMenu(forSelectionType: ItemModel.ID.self) { items in
// ...
} primaryAction: { items in
// This is executed when the row is double clicked
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
您必须更改Set<Value.ID>为Value.ID仅一行选择,并TapGesture在文本中进行。
@State private var selection = Set<ItemModel.ID>() // <-- Use this for multiple rows selections
Run Code Online (Sandbox Code Playgroud)
@State private var selection : ItemModel.ID? // <--- Use this for only one row selection
Run Code Online (Sandbox Code Playgroud)
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
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4508 次 |
| 最近记录: |