NSTableView 使用箭头键导航

发布于 2024-08-31 07:18:17 字数 117 浏览 12 评论 0原文

如何使用箭头键浏览表格视图。与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

濫情▎り 2024-09-07 07:18:17

在表视图委托中,实现 tableView:shouldSelectRow:。做任何你想做的事,然后返回YES。当您在表视图中选择项目时,它将被触发。

In your table view delegate, implement tableView:shouldSelectRow:. Do whatever you want, then return YES. It'll get triggered as you select items in the table view.

南城追梦 2024-09-07 07:18:17

我不确定您的意思,因为当我在表格中选择某些内容时,我可以使用箭头键在表格中上下移动。但如果你想更多地定制行为,我有一个解决方案。在我的一个应用程序中,我想检测何时按下返回键或回车键,然后相应地执行一些操作。我创建了一个新类并将其作为 NSWindow 的子类。在界面生成器中,我将主窗口设置为此类。然后我重写该子类中 NSWindow 的 keyDown: 方法。因此,每当我的主窗口位于最前面(第一响应者)时,就会通过该方法检测并过滤按键。我相信你可以对箭头按下做类似的事情。您可能希望使您的类成为 NSTableView 而不是 NSWindow 的子类,具体取决于您希望如何捕获按键。我希望它适用于整个应用程序,但您可能希望它仅在表视图是第一响应者时才起作用。

- (void)keyDown:(NSEvent *)theEvent {
    if ([theEvent type] == NSKeyDown) {
        NSString* characters = [theEvent characters];
        if (([characters length] > 0) && (([characters characterAtIndex:0] == NSCarriageReturnCharacter) || ([characters characterAtIndex:0] == NSEnterCharacter))) {
            // do something here when return or enter is pressed
        }
    }
}

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.

- (void)keyDown:(NSEvent *)theEvent {
    if ([theEvent type] == NSKeyDown) {
        NSString* characters = [theEvent characters];
        if (([characters length] > 0) && (([characters characterAtIndex:0] == NSCarriageReturnCharacter) || ([characters characterAtIndex:0] == NSEnterCharacter))) {
            // do something here when return or enter is pressed
        }
    }
}
赠佳期 2024-09-07 07:18:17

啊哈!你这样做是否不小心破坏了 NSTableView ?

@implementation NSTableView ( DeleteKeyCategory )
-( void ) keyDown: ( NSEvent * ) event
{
   // ... do something ...

   [super keyDown:event];
}
@end

对我来说,这具有替换 NSTableView 的 keyDown: 例程的邪恶副作用,并且它破坏了光标键。 (有点像调酒)

我学到的教训:
- 完全避免 keyDown: 例程。
- 从长远来看,子类化 Apple NSControls 将节省工作量。

这种错误使得使用 NSTableView 非常令人沮丧。
也许苹果可以在静态分析器中检测到这种东西?

aha! Did you accidentally BREAK NSTableView by doing this?

@implementation NSTableView ( DeleteKeyCategory )
-( void ) keyDown: ( NSEvent * ) event
{
   // ... do something ...

   [super keyDown:event];
}
@end

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?

乙白 2024-09-07 07:18:17

箭头用于选择,而不是用于执行任何操作。将应用于所选项目的操作通常由 TableView 的“action”或“doubleAction”属性设置。

单击表行会执行两种不同的操作。

  1. 尝试选择表格行(有时表格行可以拒绝选择,这就是为什么有“shouldSelect”委托方法)。

  2. 如果发生新选择,则执行操作(以 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.

  1. TRIES to select the table row (sometimes the table-row can REFUSE to be selected, that's why there is a "shouldSelect" delegate method).

  2. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文