NSTableView hit tab to jump from row to row while editing

后端 未结 2 513
难免孤独
难免孤独 2021-02-10 06:27

I\'ve got an NSTableView. While editing, if I hit tab it automatically jumps me to the next column. This is fantastic, but when I\'m editing the field in the last column and I h

相关标签:
2条回答
  • 2021-02-10 07:08

    You can do this without subclassing by implementing 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;
        }   
    }
    

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

    This method responds to the actual keypress, so you're not constrained to the textDidEndEditing method, which only works for text cells.

    0 讨论(0)
  • 2021-02-10 07:25

    Subclass UITableView and add code to catch the textDidEndEditing call.

    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 isn't tested... no warranties... etc. And you might need to move that [super textDidEndEditing:] call for the tab-in-the-right-most-cell case. But hopefully this will help you to the finish line. Let me know!

    0 讨论(0)
提交回复
热议问题