NSTableView 不响应向上/向下箭头键
我有一个 NSTableView 不响应向上/向下箭头键。根据我的理解,响应是默认行为,所以我不确定发生了什么(注意,我不会覆盖任何键盘命令)。我为我的桌子做的唯一设置是:
[tableView setDoubleAction: @selector(doubleClickedRow)];
[tableView setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[tableView registerForDraggedTypes: [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
[tableView setDraggingSourceOperationMask: NSDragOperationCopy forLocal:NO];
但据我所知,这些都不会导致任何问题。
有人对我可以在哪里寻找这个问题的根源有任何建议吗?
I have an NSTableView that is not responding to up/down arrows keys. From my understanding responding IS the default behaviour, so I'm not sure what is going on (Note, I am NOT overriding any keyboard commands). The only setup I do for my table is:
[tableView setDoubleAction: @selector(doubleClickedRow)];
[tableView setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[tableView registerForDraggedTypes: [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
[tableView setDraggingSourceOperationMask: NSDragOperationCopy forLocal:NO];
But from what I can tell, none of these would cause any problems.
Does anyone have any suggestions on where I could look for the root of this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果包含表格的 NSWindow 不是关键窗口,则 NSTableView 也将不会响应箭头键。
我最近遇到的一种情况是,如果 NSWindow 的“标题栏”外观选项未在 Interface Builder 中设置:这会更改 NSWindow 的默认行为以拒绝按键状态。请参阅
-[NSWindow canBecomeKeyWindow]
。因此,即使 NSWindow 显示为没有标题栏的模式表,也必须设置标题栏选项,否则按键事件将无法到达 NSTableView。
An NSTableView will also be unresponsive to arrow keys if the NSWindow that contains the table is not the key window.
A situation where this can occur which I recently ran into is if the NSWindow's "Title Bar" appearance option is not set in Interface Builder: that changes the default behaviour of NSWindow to refuse key status. See
-[NSWindow canBecomeKeyWindow]
.Thus even where the NSWindow is presented as a modal sheet without a title bar, the Title Bar option must be set or key events won't reach the NSTableView.
您是否在任何地方重写了
keyDown:
?是在自定义 NSTableView 子类中还是在窗口控制器中?这可能是箭头键在您的表格中不起作用的原因。我在自定义表格视图时遇到了同样的问题。我通过调用方向键的 super 来让表处理事件来解决这个问题:
Did you override
keyDown:
anywhere? Either in a customNSTableView
sub-class or maybe in the window controller? This could be the cause whey the arrow keys don't work in your table.I ran into the same problem with a custom table view. I solved it by calling
super
for the arrow keys to let the table handle the event:这不是默认行为。 NSTableView 确实处理键盘事件并允许使用光标键进行导航。
也许您可以使用以下问题中的技巧来调试响应者链:
如何检查响应者链?
This is not default behavior. NSTableView does handle keyboard events and allows navigation with the cursor keys.
Maybe you can debug the responder chain with the trick in the following question:
How to inspect the responder chain?
刚刚遇到这个问题,我发现这条线阻止了向上/向下箭头键正确选择行。
[self.tableView setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleNone];
我试图使用自己的背景颜色,所以我添加了它。当我点击向上/向下时,它触发了
shouldSelectRow:
,但它始终是 0!注释掉上面的行为我解决了这个问题。Just ran into this problem, and I found this line prevents the up/down arrow keys from selecting rows correctly.
[self.tableView setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleNone];
I was trying to use my own background color so I added that. And when I tapped on up/down, it triggered
shouldSelectRow:
, but it was always 0! Commenting out the above line fixed it for me.我遇到了同样的问题,使用箭头键我的表视图的委托没有收到更改通知。
这是我的 Swift 解决方案:
首先对 NSTableView 进行子类化,然后重写此方法。
123至126码分别为Left、Right、Down、Up
I had the same problem, that with the arrow keys my tableview's delegate didn't get notified of the change.
This was my Swift solution:
First sub-classing NSTableView and then overriding this method.
The 123 to 126 codes are Left, Right, Down and Up respectively