One Quickie

Disabling "Return moves editing to next cell" in TableView (NSTableView->General)
When you edit cells in a tableview, pressing return, tab, or shift-tab will end the current editing (which is good), and starts editing the next cell. But of times you don't want that to happen - the user wants to edit an attribute of a given row, but it doesn't ever want to do batch changes to everything.

To make editing end, you need to subclass NSTableView and add code to catch the textDidEndEditing delegate notification, massage the text movement value to be something other than the return and tab text movement, and then let NSTableView handle things.

// make return and tab only end editing, and not cause other cells to edit

- (void) textDidEndEditing: (NSNotification *) notification
    NSDictionary *userInfo = [notification userInfo];

    int textMovement = [[userInfo valueForKey:@"NSTextMovement"] intValue];

    if (textMovement == NSReturnTextMovement
        || textMovement == NSTabTextMovement
        || textMovement == NSBacktabTextMovement) {

        NSMutableDictionary *newInfo;
        newInfo = [NSMutableDictionary dictionaryWithDictionary: userInfo];

        [newInfo setObject: [NSNumber numberWithInt: NSIllegalTextMovement]
                 forKey: @"NSTextMovement"];

        notification =
            [NSNotification notificationWithName: [notification name]
                                       object: [notification object]
                                       userInfo: newInfo];


    [super textDidEndEditing: notification];
    [[self window] makeFirstResponder:self];

} // textDidEndEditing
(Thanks to Steven Jacowski for a tweak that ends editing on clicks on different cells)

borkware home | products | miniblog | rants | quickies | cocoaheads
Advanced Mac OS X Programming book