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

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

问题描述

我的应用程式有一个 NSOutlineView NSTableView ,我有两个相同的问题。如果某一行处于选中状态,按Tab键将使第一列进入编辑模式,而不是让下一个键视图成为第一个响应者。要进入下一个关键视图,您需要在所有列中选择所有列。



此外,shift-tabbing到任一视图都会产生列进入编辑模式,需要更多的shift-tabs进入其以前的键视图。



如果情况重要,我使用自动计算键视图循环,而不是我自己的,我的 NSWindow 设置为 autorecalculatesKeyViewLoop = YES 。我想在用户选择编辑列时在列之间进行选项卡,但我不认为这是标签键的触发编辑模式的标准行为。



更新



感谢以下有用的回应,我完成了工作。基本上,我在我的自定义表视图类中覆盖 -keyDown ,它处理表格视图中的选项卡和移动选项。然而,解决移位到表视图更困难。如果它从另一个视图接受控制,我在自定义表视图的 -acceptsFirstResponder 中设置一个布尔属性到 YES p>

委托的 -tableView:shouldEditTableColumn:row 检查当前事件是否为移动标签 keyDown 事件。 -tableView:shouldEditTableColumn:row 被调用,它不是一个shift-tab事件,它将表视图的属性设置为 NO

我已经粘贴了以下的完整解决方案。

  / * 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
{
//处理Tab键
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键切换到下一个键视图。如果已选择行,则表格视图开始编辑,或者如果已经编辑,则移动到下一个单元格。



AppKit发行说明


inter-cell
导航如下:




  • 向前转到一个表格会集中整个表格。

  • 如果在该行中只有一个
    实例,则击中空格将尝试在
    中的一个NSButtonCell上执行performClick:。


  • 如果可以编辑新聚焦的单元格,则会开始编辑。

  • 如果更改,点击空间调用单元格上的performClick:,然后设置数据源
    的值。 (2)

  • 如果文本单元格正在编辑,按Enter键将提交编辑并将焦点
    返回到tableview,
    Tab / Shift-tab



  • >一旦到达一行中的最后一个单元格,tab将把焦点转移到下一个可聚焦控件的

  • 返回到表格中将选择最后一个可聚焦单元格。 / li>

如果要更改此行为,委托方法 tableView:shouldEditTableColumn :row:可能会有所帮助。如果您确实只想影响Tab键的行为,您还可以将 NSTableView 子类化。


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.

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.

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.

Update

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.

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;
}

. . .

解决方案

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.

From AppKit Release Notes:

Tables now support inter-cell navigation as follows:

  • Tabbing forward to a table focuses the entire table.
  • Hitting Space will attempt to 'performClick:' on a NSButtonCell in the selected row, if there is only one instance in that row.
  • Tabbing again focuses the first "focusable" (1) cell, if there is one.
  • If the newly focused cell can be edited, editing will begin.
  • Hitting Space calls 'performClick:' on the cell and sets the datasource value afterwards, if changed. (2)
  • If a text cell is editing, hitting Enter will commit editing and focus will be returned to the tableview, and Tab/Shift-tab will commit the editing and then perform the new tab-loop behavior.
  • Tabbing will only tab through a single row
  • Once the last cell in a row is reached, tab will take the focus to the next focusable control.
  • Back tabbing into a table will select the last focusable cell.

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& NSOutlineView在标签键上编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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