NSTableView &NSOutlineView 在 tab 键上编辑 [英] NSTableView & NSOutlineView editing on tab key

查看:22
本文介绍了NSTableView &NSOutlineView 在 tab 键上编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用有一个 NSOutlineView 和一个 NSTableView,我在这两个方面都遇到了同样的问题.选中任一行后,按 Tab 键会将第一列置于编辑模式,而不是让下一个键查看第一响应者.要进入下一个关键视图,您需要在所有列之间切换.

My app has an NSOutlineView and an NSTableView, and I'm having the same problem with both. With a row in either selected, pressing the tab key puts the first column into edit mode instead of making the next key view first responder. To get to the next key view, you need to tab through all of the columns.

此外,切换到任一视图会导致 last 列进入编辑模式,需要更多的切换选项卡才能进入其先前的键视图.

Also, shift-tabbing into either view results in the last column going into edit mode, necessitating more shift-tabs to get into its previous key view.

以防万一,我使用的是自动计算的键视图循环,而不是我自己的,我的 NSWindow 设置为 autorecalculatesKeyViewLoop = YES.一旦用户选择编辑列,我想在列之间切换,但我不认为这是 trigger 编辑模式的 tab 键的标准行为.

In case it matters, I'm using the autocalculated key view loop, not my own, with my NSWindow set to autorecalculatesKeyViewLoop = YES. I would like tabbing between the columns once the user elects to edit a column, but I don't think it's standard behavior for the tab key to trigger edit mode.

更新

感谢以下有用的回复,我解决了.基本上,我在我的自定义表格视图类中覆盖了 -keyDown,该类处理表格视图中的制表符和移位制表符.然而,解决表格视图中的 shift-tabbing 问题更加困难.如果自定义表视图的 -acceptsFirstResponder 接受来自另一个视图的控制,我将布尔属性设置为 YES.

Thanks to the helpful responses below, I worked it out. Basically, I override -keyDown in my custom table view class, which handles tabbing and shift-tabbing out of the table view. It was tougher to solve shift-tabbing into the table view, however. I set a boolean property to YES in the custom table view's -acceptsFirstResponder if it's accepting control from another view.

委托的 -tableView:shouldEditTableColumn:row 在当前事件是 shift-tab keyDown 事件时进行检查.-tableView:shouldEditTableColumn:row 被调用,它不是一个 shift-tab 事件,它将表格视图的属性设置回 NO 所以它仍然可以像往常一样编辑.

The delegate's -tableView:shouldEditTableColumn:row checks for that when the current event is a shift-tab keyDown event. -tableView:shouldEditTableColumn:row is called and it's not a shift-tab event, it sets the table view's property back to NO so it can still be edited as usual.

我在下面粘贴了完整的解决方案.

I've pasted the full solution below.

/* CustomTableView.h */

@interface CustomTableView : NSTableView {}

@property (assign) BOOL justFocused;

@end

<小时>

/* CustomTableView.m */

@implementation CustomTableView

@synthesize justFocused;

- (BOOL)acceptsFirstResponder {
    if ([[self window] firstResponder] != self) {
        justFocused = YES;
    }

    return YES;
}

- (void)keyDown:(NSEvent *)theEvent
{
    // Handle the Tab key
    if ([[theEvent characters] characterAtIndex:0] == NSTabCharacter) {
        if (([theEvent modifierFlags] & NSShiftKeyMask) != NSShiftKeyMask) {
            [[self window] selectKeyViewFollowingView:self];
        } else {
            [[self window] selectKeyViewPrecedingView:self];
        }
    }
    else {
        [super keyDown:theEvent];
    }
}

@end

<小时>

/* TableViewDelegate.m */

. . .

- (BOOL)tableView:(NSTableView *)tableView shouldEditTableColumn:(NSTableColumn *)tableColumn
              row:(NSInteger)row
{
    NSEvent *event = [NSApp currentEvent];
    BOOL shiftTabbedIn = ([event type] == NSKeyDown
                          && [[event characters] characterAtIndex:0] == NSBackTabCharacter);

    if (shiftTabbedIn && ((CustomTableView *)tableView).justFocused == YES) {
        return NO;
    } else {
        ((CustomTableView *)tableView).justFocused = NO;
    }

    return YES;
}

. . .

推荐答案

这是默认行为.如果没有选择行,则整个表格视图具有焦点,并且 Tab 键切换到下一个键视图.如果选中了一行,表格视图将开始编辑,如果已经在编辑,则移动到下一个单元格.

This is the default behavior. If there's no row selected, the table view as a whole has focus, and the Tab key switches to the next key view. If there is a row selected, the table view begins editing or moves to the next cell if already editing.

来自 AppKit 发行说明:

表格现在支持单元格间导航如下:

Tables now support inter-cell navigation as follows:

  • 向前跳到一个表格会聚焦整个表格.
  • Hitting Space 将尝试在 NSButtonCell 上执行 'performClick:'选定的行,如果只有一个该行中的实例.
  • 再次使用 Tab 键聚焦第一个可聚焦"(1) 单元格(如果有的话).
  • 如果可以编辑新聚焦的单元格,则开始编辑.
  • Hitting Space 在单元格上调用performClick:"并设置数据源之后的值,如果更改.(2)
  • 如果文本单元格正在编辑,按 Enter 将提交编辑并获得焦点将返回到 tableview,并且Tab/Shift-tab 将提交编辑然后执行新的标签循环行为.
  • 制表符只会在单行中制表符
  • 到达一行中的最后一个单元格后,标签会将焦点移至下一个可聚焦控件.
  • 返回到表格中的 Tab 键将选择最后一个可聚焦的单元格.

如果您想更改此行为,委托方法 tableView:shouldEditTableColumn:row: 可能会有所帮助.如果您真的只想影响 Tab 键的行为,您可能还必须继承 NSTableView.

If you want to change this behavior, the delegate method tableView:shouldEditTableColumn:row: may be helpful. You may also have to subclass NSTableView if you really want to affect only the behavior of the Tab key.

这篇关于NSTableView &amp;NSOutlineView 在 tab 键上编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆