带NSTableView的箭头键 [英] Arrow keys with NSTableView
问题描述
是否可以使用箭头键导航NSTableView的NSTableView周围的可编辑单元格,并输入/ tab?例如,我想让它感觉更像一个电子表格。
Is it possible to navigate an NSTableView's editable cell around the NSTableView using arrow keys and enter/tab? For example, I want to make it feel more like a spreadsheet.
这个应用程序的用户需要编辑相当多的单元格(但不是所有的)
The users of this application are expected to edit quite a lot of cells (but not all of them), and I think it would be easier to do so if they didn't have to double-click on each cell.
推荐答案
如果不需要双击每个单元格,这样做会更容易。这不容易,但我设法做到,而不必使用RRSpreadSheet或甚至另一个控件。这里是你必须做的:
Well it isn't easy but I managed to do it without having to use RRSpreadSheet or even another control. Here's what you have to do:
-
创建
NSTextView
,这将是字段编辑器。对于这个例子,将使用名称MyFieldEditorClass
,myFieldEditor
将引用此类的实例。
Create a subclass of
NSTextView
, this will be the field editor. For this example the nameMyFieldEditorClass
will be used andmyFieldEditor
will refer to an instance of this class.
添加方法到 MyFieldEditorClass
调用 - (void)setLastKnownColumn:(unsigned)aCol
Add a method to MyFieldEditorClass
called "- (void) setLastKnownColumn:(unsigned)aCol andRow:(unsigned) aRow
" or something similar, and have it save both the input parameter values somewhere.
添加另一个方法称为setTableView:,并将NSTableView对象保存在某个地方,或者除非有其他方法从字段编辑器获取NSTableView对象。
Add another method called "setTableView:" and have it save the NSTableView object somewhere, or unless there is another way to get the NSTableView object from the field editor, use that.
添加另一个名为 - (void)keyDown:(NSEvent *)event
的方法。这实际上覆盖了 NSResponder
的 keyDown:
。源代码应该是(注意StackOverflow的MarkDown正在改变<
和>
到& lt;
和& gt;
):
Add another method called - (void) keyDown:(NSEvent *) event
. This is actually overriding the NSResponder
's keyDown:
. The source code should be (be aware that StackOverflow's MarkDown is changing <
and >
to <
and >
):
- (void) keyDown:(NSEvent *) event
{
unsigned newRow = row, newCol = column;
switch ([event keyCode])
{
case 126: // Up
if (row)
newRow = row - 1;
break;
case 125: // Down
if (row < [theTable numberOfRows] - 1)
newRow = row + 1;
break;
case 123: // Left
if (column > 1)
newCol = column - 1;
break;
case 124: // Right
if (column < [theTable numberOfColumns] - 1)
newCol = column + 1;
break;
default:
[super keyDown:event];
return;
}
[theTable selectRow:newRow byExtendingSelection:NO];
[theTable editColumn:newCol row:newRow withEvent:nil select:YES];
row = newRow;
column = newCol;
}
在您的nib中授予NSTableView一个委托,添加方法:
Give the NSTableView in your nib a delegate, and in the delegate add the method:
- (BOOL) tableView:(NSTableView *)aTableView shouldEditColumn:(NSTableColumn *) aCol row:aRow
{
if ([aTableView isEqual:TheTableViewYouWantToChangeBehaviour])
[myFieldEditor setLastKnownColumn:[[aTableView tableColumns] indexOfObject:aCol] andRow:aRow];
return YES;
}
最后,给Table View的主窗口一个委托,方法:
Finally, give the Table View's main window a delegate and add the method:
- (id) windowWillReturnFieldEditor:(NSWindow *) aWindow toObject:(id) anObject
{
if ([anObject isEqual:TheTableViewYouWantToChangeBehaviour])
{
if (!myFieldEditor)
{
myFieldEditor = [[MyFieldEditorClass alloc] init];
[myFieldEditor setTableView:anObject];
}
return myFieldEditor;
}
else
{
return nil;
}
}
运行程序,并给它一个去!
Run the program and give it a go!
这篇关于带NSTableView的箭头键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!