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.
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:
Create a subclass of
NSTextView, this will be the field editor. For this example the nameMyFieldEditorClasswill be used andmyFieldEditorwill refer to an instance of this class.Add a method to
MyFieldEditorClasscalled ‘- (void) setLastKnownColumn:(unsigned)aCol andRow:(unsigned) aRow‘ or something similar, and have it save both the input parameter values somewhere.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.
Add another method called
- (void) keyDown:(NSEvent *) event. This is actually overriding theNSResponder‘skeyDown:. The source code should be (be aware that StackOverflow’s MarkDown is changing<and>to<and>):Give the NSTableView in your nib a delegate, and in the delegate add the method:
Finally, give the Table View’s main window a delegate and add the method:
Run the program and give it a go!