NSTableView tab to go from line to line when editing

I have an NSTableView. During editing, if I click a tab, it automatically moves to the next column. This is fantastic, but when I edit the field in the last column and I click on the tab, I would like to focus to go to the first column of the NEXT row.

Any suggestions?

Thanks to Michael for the starter code, it was very close to what ended up working! Here is the last code I used, hope it will be useful to someone else:

- (void) textDidEndEditing: (NSNotification *) notification { NSInteger editedColumn = [self editedColumn]; NSInteger editedRow = [self editedRow]; NSInteger lastColumn = [[self tableColumns] count] - 1; NSDictionary *userInfo = [notification userInfo]; int textMovement = [[userInfo valueForKey:@"NSTextMovement"] intValue]; [super textDidEndEditing: notification]; if ( (editedColumn == lastColumn) && (textMovement == NSTabTextMovement) && editedRow < ([self numberOfRows] - 1) ) { // the tab key was hit while in the last column, // so go to the left most cell in the next row [self selectRowIndexes:[NSIndexSet indexSetWithIndex:(editedRow+1)] byExtendingSelection:NO]; [self editColumn: 0 row: (editedRow + 1) withEvent: nil select: YES]; } } 
+4
source share
2 answers

Subclass UITableView and add code to catch a call to textDidEndEditing.

You can then decide what to do based on something like this:

 - (void) textDidEndEditing: (NSNotification *) notification { NSDictionary *userInfo = [notification userInfo]; int textMovement = [[userInfo valueForKey:@"NSTextMovement"] intValue]; if ([self selectedColumn] == ([[self tableColumns] count] - 1)) (textMovement == NSTabTextMovement) { // the tab key was hit while in the last column, // so go to the left most cell in the next row [yourTableView editColumn: 0 row: ([self selectedRow] + 1) withEvent: nil select: YES]; } [super textDidEndEditing: notification]; [[self window] makeFirstResponder:self]; } // textDidEndEditing 

This code has not been tested ... no guarantees ... etc. And you may need to move this call [super textDidEndEditing:] for the case of a tab in the right block. But hopefully this will help you at the finish. Let me know!

+1
source

You can do this without a subclass by running control:textView:doCommandBySelector:

 -(BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector { if(commandSelector == @selector(insertTab:) ) { // Do your thing return YES; } else { return NO; } } 

( NSTableViewDelegate implements the NSControlTextEditingDelegate protocol, where this method is defined)

This method responds to actual keystrokes, so you are not limited to the textDidEndEditing method, which only works for text cells.

+2
source

All Articles