2013-07-18 187 views
1

實施帶有可編輯標題的NSTableView,如this Stackoverflow question。一切正常,除了雙擊表格編輯單元格。這不再有效。NSTableView可編輯標題並在表格中雙擊單元格

我懷疑這是因爲該NSTableView setDoubleAction:位被設置爲自定義方法,然後該方法成爲表中的所有雙擊事件處理程序(從原來的問題代碼示例如下):

-(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 
} 

我的問題是,如何恢復編輯表格單元格的雙擊功能?

謝謝。

回答

1

找到答案准備的問題後不久,並認爲這可能是問答& A.

doubleClickInTableView:方法,有檢查,以確保被點擊標題單元格。因此,用else子句擴展該檢查就足夠了,並執行editColumn:row:withEvent:select:

-(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]; 
    } 
} 
相關問題