NSOutlineView:如何让父项加粗?

时间:2014-08-01 08:39:28

标签: objective-c macos cocoa

我有一个NSOutlineView,其中包含父项和子项列表。

使用粗体文字制作父项(带有左箭头的那些项)的最简单方法是什么?

感谢

1 个答案:

答案 0 :(得分:2)

如果您使用的是基于单元格的大纲视图,则可以使用此委托方法来粗体显示父项:

// Assuming you use NSTextFieldCell
- (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item {
    if ([self outlineView:outlineView numberOfChildrenOfItem:item] > 0) {
        [(NSCell *)cell setFont: [NSFont boldSystemFontOfSize: ((NSCell *)cell).font.pointSize]];
    } else {
        [(NSCell *)cell setFont: [NSFont systemFontOfSize: ((NSCell *)cell).font.pointSize]];
    }
}

否则,在基于视图的大纲视图中,您可以执行以下操作:

- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item {
    NSTableCellView *cellView = [outlineView makeViewWithIdentifier:@"MyIdentifier" owner:self];
    if ([self outlineView:outlineView numberOfChildrenOfItem:item] > 0) {
        [cellView.textField setFont: [NSFont boldSystemFontOfSize: cellView.textField.font.pointSize]];
    } else {
        [cellView.textField setFont: [NSFont systemFontOfSize: cellView.textField.font.pointSize]];
    }

    // do whatever else you do
    return cellView;
}

或者,NSOutlineViewDelegate' -outlineView:isGroupItem:可能是您实际需要的内容:https://developer.apple.com/library/mac/documentation/cocoa/reference/NSOutlineViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/NSOutlineViewDelegate/outlineView:isGroupItem