objective-ccocoanstableview

NSTableView Loses Focus After Calling editColumn


I have an NSTableView where the user clicks a button to add a new row. This goes to an IBAction that inserts an object in the NSArrayController and then uses editColumn:row:withEvent:select: to put the new row into edit mode for the user.

The problem is that once editColumn:row:withEvent:select: is fired all rows in the tableview turn gray, ie the tableview loses focus. I've tried reseting first responder before and after calling editColumn but nothing works. Once the new row is inserted and everything turns gray I have to select rows one at a time before their text will turn normal (black) again.

How can I programmatically set a row/column to edit mode without this happening?

-(IBAction)addAttribute:(id)sender
    {
    TKSpecialObject *theObject = [TKSpecialObject new];
    [self.myArrayController insertObject:theObject atArrangedObjectIndex:self.myArrayController.count];
    [self.view.window makeFirstResponder:theTableView];
    [self.myTableView editColumn:0 row:[self.myTableView selectedRow] withEvent:nil select:YES];  
    }

enter image description here


Solution

  • @Willeke pointed out in a comment above that there is a thread regarding this behavior – NSArrayController 'Add' function now causes saved NSTableView listed entries to temporarily lose focus. In that thread the accepted answer instructs you to sub-class a private method in NSTextFieldCell. However, I didn't need to do that because this only happens when the edited cell is empty. I simply added a string value of "untitled" to the property of the object that is bound to the cell and all works as expected.

    -(IBAction)addAttribute:(id)sender
        {
        TKSpecialObject *theObject = [TKSpecialObject new];
        [TKSpecialObject setMyTitle:@"untitled"]; // added this line to fix
        [self.myArrayController insertObject:theObject atArrangedObjectIndex:self.myArrayController.count];
        [self.view.window makeFirstResponder:theTableView];
        [self.myTableView editColumn:0 row:[self.myTableView selectedRow] withEvent:nil select:YES];  
        }