2014-10-08 116 views
0

我想創建一個非常簡單的NSTableCellView與文本框和按鈕。我的代碼佈局NSTableCellView如下。我希望textfield的寬度爲100px。NSTableCellView內NSTextField的寬度

- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item { 

    MyTableCellView *cellView = [outlineView makeViewWithIdentifier:@"MyTableCellView" owner:self]; 

    if (cellView == nil) { 
     cellView = [[MyTableCellView alloc] initWithFrame:NSMakeRect(0, 0, NSWidth(outlineView.bounds), [outlineView rowHeight])]; 

     NSTextField *textField = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 100, [outlineView rowHeight])]; 
     [cellView addSubview:textField]; 
     cellView.textField = textField; 

     NSButton *button = [[NSButton alloc] initWithFrame:NSMakeRect(108, 0, 50, [outlineView rowHeight])]; 
     [cellView addSubview:button]; 
     cellView.button = button; 

     cellView.identifier = @"MyTableCellView"; 
    } 

    cellView.textField.stringValue = item.stringValue; 

    return cellView; 
} 

當我這樣做,但是,的NSTextField佔據了整個行:

我在做什麼錯?

回答

0

我發現了這個問題。我沒有考慮將我創建的NSTextField設置到NSTableCellView的textfield插座。事實證明,這不應該做,因爲它會自動調整大小。

於是我拿出了這條線,它按預期工作:

cellView.textField = textField;