NSTableCellView中NSTextField的宽度

时间:2014-10-08 18:15:54

标签: cocoa nsoutlineview nstablecellview

我正在尝试使用文本字段和按钮创建一个非常简单的NSTableCellView。我的NSTableCellView布局代码如下。我希望textfield的宽度为100px。

- (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会占用整行:

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我发现了问题。我没有想过将我创建的NSTextField设置为NSTableCellView的textfield outlet。事实证明这不应该做,因为它会自动调整大小。

所以我拿出这一行,它按预期工作:

cellView.textField = textField;