Arrow keys with NSTableView

后端 未结 4 1043
闹比i
闹比i 2020-12-08 16:50

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

4条回答
  •  囚心锁ツ
    2020-12-08 17:37

    I wanted to reply to the answers here but the reply button seems to be missing so I'm forced to proved an answer when I really just want to ask a question about the replies.

    Anyway, I've seen a few answers for overriding the -keyDown event of the table view that say to subclass the TableView but according to every Objective-C book I've read so far, and several Apple training videos, you should very rarely if ever subclass one of the core classes. In fact every single one of them makes the point that C programmers have a fascination with subclassing and that's not how Objective-C works; that Objective-C is all about helpers and delegates not subclassing.

    So, should I just ignore any of the responses that say to subclass as this seems to be in direct contradiction to the precepts of Objective-C?

    --- Edit ---

    I found something that worked without subclassing the NSTableView. While I do move the inheritance up one notch on the chain from NSObject to NSResponder I'm not totally subclassing the NSTableView. I'm just adding the ability to override the keyDown event.

    I made the class I was using as a delegate inherit from NSResponder instead of NSObject and set the nextResponder to that class in awakeFromNib. I was then able to trap key presses using the keydown event. I of course connected the IBOutlet and set the delegate in Interface Builder.

    Here's my code with the minimum needed to show the trapping of the key:

    Header file

    //  AppController.h
    
    #import 
    
    @interface AppController : NSResponder {
    
        IBOutlet NSTableView *toDoListView;
        NSMutableArray *toDoArray;
    }
    
    -(int)numberOfRowsInTableView:(NSTableView *)aTableView;
    
    -(id)tableView:(NSTableView *)tableView
    objectValueForTableColumn:(NSTableColumn *)aTableColumn
               row:(int)rowIndex;
    
    @end
    

    Here's the m file.

    //  AppController.m
    #import "AppController.h"
    
    @implementation AppController
    
    -(id)init
    {
        [super init];
        toDoArray = [[NSMutableArray alloc] init];
        return self;
    }
    
    -(void)dealloc
    {
        [toDoArray release];
        toDoArray = nil;
        [super dealloc];
    }
    
    -(void)awakeFromNib
    {
        [toDoListView setNextResponder:self];
    }
    
    -(int)numberOfRowsInTableView:(NSTableView *)aTableView
    {
        return [toDoArray count];
    }
    
    -(id)tableView:(NSTableView *)tableView
        objectValueForTableColumn:(NSTableColumn *)aTableColumn
                              row:(int)rowIndex
    {
        NSString *value = [toDoArray objectAtIndex:rowIndex];
        return value;
    }
    
    - (void)keyDown:(NSEvent *)theEvent
    {
        //NSLog(@"key pressed: %@", theEvent);
        if (theEvent.keyCode == 51 || theEvent.keyCode == 117)
        {
            [toDoArray removeObjectAtIndex:[toDoListView selectedRow]];
            [toDoListView reloadData];
        }
    }
    @end
    

提交回复
热议问题