Abc*_*Efg 14 cocoa objective-c nstableview
我正在寻找一种从右键单击行索引的方法,NSTableView但我找不到任何委托方法或类属性.任何建议表示赞赏.
Gra*_*iln 21
使用NSTableView方法- (NSInteger)clickedRow获取最后单击的行的索引.返回的NSInteger将是右键单击行的索引.
您不需要NSTableView为此解决方案创建子类.clickedRow也可以NSOutlineView.
Pet*_*ese 11
虽然我没有这样做,我可以很肯定通过重写NSView的- (NSMenu*)menuForEvent:(NSEvent*)theEvent.此链接中的示例执行点转换以确定索引.
-(NSMenu*)menuForEvent:(NSEvent*)theEvent
{
NSPoint mousePoint = [self convertPoint:[theEvent locationInWindow] fromView:nil];
int row = [self rowAtPoint:mousePoint];
// Produce the menu here or perform an action like selection of the row.
}
Run Code Online (Sandbox Code Playgroud)
如果您想在菜单打开时点击行索引,答案是NSTableView.clickedRow. 无论如何,此属性仅在特定时刻可用,并且通常仅-1.
这个索引什么时候可用?那是NSMenuDelegate.menuWillOpen方法论。因此,您符合委托并在您的类上实现该方法,并访问该clickedRow属性。完成。
final class FileNavigatorViewController: NSViewController, NSMenuDelegate {
let ov = NSOutlineView() // Assumed you setup this properly.
let ctxm = NSMenu()
override func viewDidLoad() {
super.viewDidLoad()
ov.menu = ctxm
ctxm.delegate = self
}
func menuWillOpen(_ menu: NSMenu) {
print(outlineView.clickedRow)
}
}
Run Code Online (Sandbox Code Playgroud)
单击的行索引可用,直到您单击菜单中的项目。所以这也有效。
final class FileNavigatorViewController: NSViewController {
let ov = NSOutlineView() // Assumed you setup this properly.
let ctxm = NSMenu()
let item1 = NSMenuItem()
override func viewDidLoad() {
super.viewDidLoad()
ov.menu = ctxm
ov.addItem(item1)
ov.target = self
ov.action = #selector(onClickItem1(_:))
}
@objc
func onClickItem1(_: NSObject?) {
print(outlineView.clickedRow)
}
}
Run Code Online (Sandbox Code Playgroud)
我在 macOS Sierra (10.12.5) 上对此进行了测试。
从 OS X 10.11 开始,Apple 终于添加了一种clickedRow轻松访问的方法。只需子类化NSTableView并覆盖这个方法,你就会得到clickedRow我所经历的。
func willOpenMenu(menu: NSMenu, withEvent event: NSEvent)
Run Code Online (Sandbox Code Playgroud)
这需要子类化,但无论如何,这是访问clickedRow.
此外,还有一种配对方法。
func didCloseMenu(menu: NSMenu, withEvent event: NSEvent?)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6588 次 |
| 最近记录: |