NSTableView 使用箭头键导航
如何使用箭头键浏览表格视图。与 setAction:
或 setDoubleAction
非常相似,但不是对点击做出反应,而是对在表格中向上或向下移动的箭头键做出反应。
How can I navigate through my table view with the arrow keys. Much like setAction:
or setDoubleAction
, but instead of reacting to clicks, react with the arrow keys moving up or down through the table.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在表视图委托中,实现
tableView:shouldSelectRow:
。做任何你想做的事,然后返回YES
。当您在表视图中选择项目时,它将被触发。In your table view delegate, implement
tableView:shouldSelectRow:
. Do whatever you want, then returnYES
. It'll get triggered as you select items in the table view.我不确定您的意思,因为当我在表格中选择某些内容时,我可以使用箭头键在表格中上下移动。但如果你想更多地定制行为,我有一个解决方案。在我的一个应用程序中,我想检测何时按下返回键或回车键,然后相应地执行一些操作。我创建了一个新类并将其作为 NSWindow 的子类。在界面生成器中,我将主窗口设置为此类。然后我重写该子类中 NSWindow 的 keyDown: 方法。因此,每当我的主窗口位于最前面(第一响应者)时,就会通过该方法检测并过滤按键。我相信你可以对箭头按下做类似的事情。您可能希望使您的类成为 NSTableView 而不是 NSWindow 的子类,具体取决于您希望如何捕获按键。我希望它适用于整个应用程序,但您可能希望它仅在表视图是第一响应者时才起作用。
I'm not sure what you mean because when I select something in a table I can move up and down in the table using the arrow keys. But if you want to customize the behavior more I have a solution. In one of my apps I wanted to detect when the return or enter key was pressed and then perform some action accordingly. I created a new class and made it a subclass of NSWindow. In interface builder I set the main window to be this class. Then I override the keyDown: method of NSWindow in that subclass. So whenever my main window is frontmost (first responder) then key presses are detected and filtered through the method. I'm sure you could do something similar for arrow presses. You might want to make your class a subclass of NSTableView instead of NSWindow depending on how you want to catch the key presses. I wanted it to work for the entire application but you may want it to work only when the table view is first responder.
啊哈!你这样做是否不小心破坏了 NSTableView ?
对我来说,这具有替换 NSTableView 的 keyDown: 例程的邪恶副作用,并且它破坏了光标键。 (有点像调酒)
我学到的教训:
- 完全避免 keyDown: 例程。
- 从长远来看,子类化 Apple NSControls 将节省工作量。
这种错误使得使用 NSTableView 非常令人沮丧。
也许苹果可以在静态分析器中检测到这种东西?
aha! Did you accidentally BREAK NSTableView by doing this?
For me, this had the nefarious side effect of REPLACING NSTableView's keyDown: routine, and it broke the cursor keys. (kind of like swizzling)
Lessons I've learned:
- avoid the keyDown: routine altogether.
- subclassing Apple NSControls will save work in the long run.
This is the type of mistake that makes using NSTableView very frustrating.
Maybe Apple could detect this kind of thing in the static analyzer?
箭头用于选择,而不是用于执行任何操作。将应用于所选项目的操作通常由 TableView 的“action”或“doubleAction”属性设置。
单击表行会执行两种不同的操作。
尝试选择表格行(有时表格行可以拒绝选择,这就是为什么有“shouldSelect”委托方法)。
如果发生新选择,则执行操作(以 tableView 作为发送者)。在那里,您可以向表询问当前选择,并对其执行任何您需要的操作。
请考虑当有多个选定的行、或选定的列或许多其他复杂情况时的情况。
在你的情况下——我建议你实现 SelectionDidChange
:(NSNotitivation)notification;
NSTableView 委托调用。这称为“选择更改后”,此时您就知道新的当前选择,并对所选项目执行任何您想要的操作。
Arrows are used for selection, not for performing any Action. The action that will be applied to the selected item will usually be set by the "action" or "doubleAction" property of the TableView.
Clicking on a table-row does two different things.
TRIES to select the table row (sometimes the table-row can REFUSE to be selected, that's why there is a "shouldSelect" delegate method).
If new selection took place, then the action is performed (with the tableView as the sender). There you can ask the table about the current selection and do whatever you need with it.
Please consider the situation when there are SEVERAL selected rows, or selected columns, or many other complicated situations.
In your case --- what I would recommend, is that you implement the
selectionDidChange:(NSNotigivation)notification;
NSTableView delegate call. This is called AFTER selection has changed, and you know by then the new current selection, and do whatever you want with the selected items.