如何在 swift5 中将 NSPopover 固定到选定的 NSTableCellView
How can I pin an NSPopover to a selected NSTableCellView in swift5
我有下面显示的代码,它将 NSPopover 固定到我的 NSTableView。我已经查看了所有 Google,所以,但是我找不到将弹出窗口固定到特定(选定)单元格的方法。
@objc func tableViewDoubleAction(发件人:NSTableCellView){
let message = "Please Choose"
let controller = NSViewController()
controller.view = NSView(frame: CGRect(x: CGFloat(100), y: CGFloat(50), width: CGFloat(300), height: CGFloat(250)))
let popover = NSPopover()
popover.contentViewController = controller
popover.contentSize = controller.view.frame.size
popover.behavior = .transient
popover.animates = true
popover.show(relativeTo: sender.bounds, of: sender as NSView, preferredEdge: NSRectEdge.maxY)
这是一张演示问题的图片:
来自 NSTableView.doubleAction
的文档:
The clickedRow and clickedColumn properties allow you to determine which row and column the double-click occurred in or if, rather than in a row, the double-click occurred in a column heading.
@objc func tableViewDoubleAction(sender: NSTableView) {
let row = sender.clickedRow
来自 clickedRow
的文档:
The index of the row the user clicked to trigger an action message. Returns –1 if the user clicked in an area of the table view not occupied by table rows.
if row >= 0 {
NSTableView
有一个方法 rect(ofRow:)
:
Returns the rectangle containing the row at the specified index.
let rowRect = sender.rect(ofRow: row)
使用此矩形定位弹出窗口
popover.show(relativeTo: rowRect, of: sender, preferredEdge: NSRectEdge.maxY)
把它们放在一起:
@objc func tableViewDoubleAction(sender: NSTableView) {
let row = sender.clickedRow
if row >= 0 {
// create the popover
let rowRect = sender.rect(ofRow: row)
popover.show(relativeTo: rowRect, of: sender, preferredEdge: NSRectEdge.maxY)
}
}
我有下面显示的代码,它将 NSPopover 固定到我的 NSTableView。我已经查看了所有 Google,所以,但是我找不到将弹出窗口固定到特定(选定)单元格的方法。
@objc func tableViewDoubleAction(发件人:NSTableCellView){
let message = "Please Choose"
let controller = NSViewController()
controller.view = NSView(frame: CGRect(x: CGFloat(100), y: CGFloat(50), width: CGFloat(300), height: CGFloat(250)))
let popover = NSPopover()
popover.contentViewController = controller
popover.contentSize = controller.view.frame.size
popover.behavior = .transient
popover.animates = true
popover.show(relativeTo: sender.bounds, of: sender as NSView, preferredEdge: NSRectEdge.maxY)
这是一张演示问题的图片:
来自 NSTableView.doubleAction
的文档:
The clickedRow and clickedColumn properties allow you to determine which row and column the double-click occurred in or if, rather than in a row, the double-click occurred in a column heading.
@objc func tableViewDoubleAction(sender: NSTableView) {
let row = sender.clickedRow
来自 clickedRow
的文档:
The index of the row the user clicked to trigger an action message. Returns –1 if the user clicked in an area of the table view not occupied by table rows.
if row >= 0 {
NSTableView
有一个方法 rect(ofRow:)
:
Returns the rectangle containing the row at the specified index.
let rowRect = sender.rect(ofRow: row)
使用此矩形定位弹出窗口
popover.show(relativeTo: rowRect, of: sender, preferredEdge: NSRectEdge.maxY)
把它们放在一起:
@objc func tableViewDoubleAction(sender: NSTableView) {
let row = sender.clickedRow
if row >= 0 {
// create the popover
let rowRect = sender.rect(ofRow: row)
popover.show(relativeTo: rowRect, of: sender, preferredEdge: NSRectEdge.maxY)
}
}