Change 'enter' key behaviour in NSTableView

我是研究僧i 提交于 2019-12-06 09:05:50

You were on the right track with the First Responder, but it's important to not that text editing is handled by the window's Field Editor (an instance of NSTextView), which becomes the first responder when text editing begins. The field editor informs its client (in this case your Table View) during editing.

The easiest way to handle this would be to subclass NSTableView and override its textDidEndEditing: method. Something like this should get you started:

- (void) textDidEndEditing:(NSNotification *)notification {
    [super textDidEndEditing:notification];

    int textMovement = [[notification.userInfo valueForKey:@"NSTextMovement"] intValue];
    if (NSReturnTextMovement == textMovement) {

        NSText *fieldEditor = notification.object;

        // The row and column for the cell that just ended editing
        NSInteger row = [self rowAtPoint:fieldEditor.frame.origin];
        NSInteger col = [self columnAtPoint:fieldEditor.frame.origin];       

        if (++col >= self.numberOfColumns) {
            col = 0;
            if (++row >= self.numberOfRows) return;
        }

        [self selectRowIndexes:[NSIndexSet indexSetWithIndex:row] 
            byExtendingSelection:NO];
        [self editColumn:col row:row withEvent:nil select:YES];
    }

}

This will progress left-to-right, top-to-bottom through the table's columns and rows. Though you said you were using a NSTextField, I presume you meant you were using a NSTextFieldCell in a cell-based table. That's another important distinction. If you're not familiar with the concept of the field editor, I suggest you read up.

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