使用 SwiftUI 的新
我嘗試將修飾符添加到TableColumn自身,但它顯示編譯錯誤:
Value of type 'TableColumn<RowValue, Never, Text, Text>' has no member 'contextMenu'
這是我在源代碼方面contextMenu的內容,在 的內容中帶有修飾符TableColumn:
struct ContentView: View {
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \Item.name, ascending: true)])
private var items: FetchedResults<Item>
@State
private var sortOrder = [KeyPathComparator(\Item.name)]
@State
private var selection = Set<Item.ID>()
var body: some View {
NavigationView {
Table(items, selection: $selection, sortOrder: $items.sortDescriptors) {
TableColumn("Column 1") {
Text("Item at \($0.name!)")
.contextMenu {
Button(action: {}) { Text("Action 1") }
Divider()
Button(action: {}) { Text("Action 2") }
Button(action: {}) { Text("Action 3") }
}
}
TableColumn("Column 2") {
Text($0.id.debugDescription)
}
}
.toolbar {
ToolbarItem {
Button(action: addItem) {
Label("Add Item", systemImage: "plus")
}
}
}
if selection.isEmpty {
Text("Select an item")
} else if selection.count == 1 {
Text("Selected \(items.first(where: { $0.id == selection.first! })!.id.debugDescription)")
} else {
Text("Selected \(selection.count)")
}
}
}
}
那么,如何將背景關系選單添加到表格內的整行?
uj5u.com熱心網友回復:
您可以使用以下擴展名制作單元格視圖并將其用于每個列單元格,然后它可以在任何行位置單擊
Text("Item at \($0.name!)")
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading) // << this !!
.contentShape(Rectangle()) // << this !!
.contextMenu {
Button(action: {}) { Text("Action 1") }
Divider()
Button(action: {}) { Text("Action 2") }
Button(action: {}) { Text("Action 3") }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/405186.html
標籤:
上一篇:為什么所有執行緒總是花費相同的時間,為什么作業系統“并行”運行所有執行緒?
下一篇:使用bash更新檔案日期/時間戳
