Skip to content

Instantly share code, notes, and snippets.

@xhruso00
Created August 29, 2014 12:48
Show Gist options
  • Save xhruso00/c382dda3f07dcb7f5897 to your computer and use it in GitHub Desktop.
Save xhruso00/c382dda3f07dcb7f5897 to your computer and use it in GitHub Desktop.
NSTableView example that treats TAB & ENTER keystroke as special command
//Subclass NSTableView and override the textDidEndEditing method. Then change the custom class of NSTableView instance in IB to the subclass.
- (void) textDidEndEditing: (NSNotification *) notification
{
NSInteger editedColumn = [self editedColumn];
NSInteger editedRow = [self editedRow];
NSInteger lastRow = [self numberOfRows];
NSInteger lastCol = [self numberOfColumns];
NSDictionary *userInfo = [notification userInfo];
int textMovement = [(NSNumber *)[userInfo valueForKey:@"NSTextMovement"] intValue];
[super textDidEndEditing: notification];
if (textMovement == NSTabTextMovement)
{
if (editedColumn != lastCol - 1 )
{
[self selectRowIndexes:[NSIndexSet indexSetWithIndex:editedRow] byExtendingSelection:NO];
[self editColumn: editedColumn+1 row: editedRow withEvent: nil select: YES];
}
else
{
if (editedRow !=lastRow-1)
{
[self editColumn:0 row:editedRow + 1 withEvent:nil select:YES];
}
else
{
[self editColumn:0 row:0 withEvent:nil select:YES]; // Go to the first cell
}
}
}
else if (textMovement == NSReturnTextMovement)
{
if(editedRow !=lastRow-1)
{
[self selectRowIndexes:[NSIndexSet indexSetWithIndex:editedRow+1] byExtendingSelection:NO];
[self editColumn: editedColumn row: editedRow+1 withEvent: nil select: YES];
}
else
{
if (editedColumn !=lastCol - 1)
{
[self editColumn:editedColumn+1 row:0 withEvent:nil select:YES];
}
else
{
[self editColumn:0 row:0 withEvent:nil select:YES]; //Go to the first cell
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment