I've implemented something similar to LTKeyPressTableView
. However, I use array controllers, so in my subclass I added IBOutlet NSArrayController * relatedArrayController
. Instead of handling delete request in a delegate, I handle it directly in the subclass since my subclass specifically deals with adding handling of Delete key. When I detect keypress for delete key, I'm just calling [relatedArrayController delete:nil];
.
IRTableView.h:
#import <Cocoa/Cocoa.h>
@interface IRTableView : NSTableView {
IBOutlet NSArrayController * relatedArrayController;
}
@end
and IRTableView.m:
#import "IRTableView.h"
@implementation IRTableView
- (void)keyDown:(NSEvent *)event
{
// Based on LTKeyPressTableView.
//https://github.com/jacobx/thoughtkit/blob/master/LTKeyPressTableView
id delegate = [self delegate];
// (removed unused LTKeyPressTableView code)
unichar key = [[event charactersIgnoringModifiers] characterAtIndex:0];
if(key == NSDeleteCharacter)
{
if([self selectedRow] == -1)
{
NSBeep();
}
BOOL isEditing = ([[self.window firstResponder] isKindOfClass:[NSText class]] &&
[[[self.window firstResponder] delegate] isKindOfClass:[IRTableView class]]);
if (!isEditing)
{
[relatedArrayController remove:nil];
return;
}
}
// still here?
[super keyDown:event];
}
@end
End result is quite IB-friendly for me, and a quite simple solution for use in a Cocoa Bindings+Core Data application.