2
votes

Implemented an NSTableView with editable headers like in this Stackoverflow question. Everything works fine, except for double-clicking in the table to edit cells. This no longer works.

I suspect this happens because of the NSTableView setDoubleAction: bit being set to the custom method, and that method then becomes the handler for all double click events in the table (code sample from original question follows):

-(void)setupTableHeader:(id)table {
    NSArray *columns = [table tableColumns];
    NSEnumerator *cols = [columns objectEnumerator];
    NSTableColumn *col = nil;

    NBETableHeaderCell *iHeaderCell;

    while (col = [cols nextObject]) {
        iHeaderCell = [[NBETableHeaderCell alloc] initTextCell:[[col headerCell] stringValue]];
        [col setHeaderCell:iHeaderCell];
        [[col headerCell] setEditable:YES];
        [iHeaderCell release];
    }
    [table setTarget:self];
    [table setDoubleAction:@selector(doubleClickInTableView:)]; // < This bit
}

My question is, how would one go about restoring the double click functionality for editing table cells?

Thank you.

1

1 Answers

2
votes

Found the answer shortly after preparing the question and thought it could be a Q&A.

In the doubleClickInTableView: method, there is a check to make sure a header cell is clicked. So, it is enough to extend that check with an else clause and implement editColumn:row:withEvent:select: there:

-(void)doubleClickInTableView:(id)sender
{
    NSInteger row = [sender clickedRow];
    NSInteger column = [sender clickedColumn];

    if(row == -1&& column >= 0)
    {
        NSTableColumn *tableColumn = [[sender tableColumns] objectAtIndex:column];
        NSTableHeaderView *headerView = [sender headerView];
        YCTableHeaderCell *headerCell = [tableColumn headerCell];

        NSWindow *window = [[NSApplication sharedApplication] mainWindow];
        id cellEditor = [window fieldEditor:YES forObject:sender];

        [headerCell setHighlighted:YES];
        [headerCell selectWithFrame:[headerView headerRectOfColumn:column]
                             inView:headerView
                             editor:cellEditor
                           delegate:headerCell
                              start:0
                             length:headerCell.stringValue.length];

        [cellEditor setBackgroundColor:[NSColor whiteColor]];
        [cellEditor setDrawsBackground:YES];
    }
    // This bit below.
    else if(row >= 0 && column >= 0)
    {
        [sender editColumn:column row:row withEvent:nil select:true];
    }
}