NSTableView hit tab to jump from row to row while editing

好久不见. 提交于 2019-12-09 06:25:29

问题


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 hit tab, I'd like focus to jump to the first column of the NEXT row.

Any suggestions?

Thanks to Michael for the starting code, it was very close to what ended up working! Here is the final code that I used, hope it will be helpful 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];
    }

}

回答1:


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!




回答2:


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.



来源:https://stackoverflow.com/questions/7536167/nstableview-hit-tab-to-jump-from-row-to-row-while-editing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!