如何在NsoutlineView中自动设置行高以适合文本



tldr:我想在NSOutlineView上实现此问题的方法(或任何其他相关方法)。

NSOutlineViewNSTableView更复杂,因为由于父/子关系,某些单元可能会缩进其他单元。

我愿意使用任何方法,包括AutoLayout,如果可能的话。我的观点是仅使用NSTextField的简单NSTableCellView实例。


我需要能够根据该行的NSTextField中的文字设置基于视图的NSOutlineView中的一行高度。

我当前正在使用以下(简化)代码,该代码使用NSString+Geometics来计算鉴定其宽度的视图的近似高度:

- (CGFloat)outlineView:(NSOutlineView *)outlineView
     heightOfRowByItem:(id)item
{
    static NSTableCellView *cell;
    if (!cell) {
        cell = [self.outlineView makeViewWithIdentifier:@"Cell"
                                                  owner:self];
    }
    NSString *text = [item text];
    float width = cell.textField.bounds.size.width;
#warning this is completely wrong due to different levels of indentation
    float height = [text heightForWidth:width];
    return height;
}

此代码背后的想法是将刮擦单元格留在周围,以便使用其默认界限属性。这适用于顶级行。

但是,NSOutlineView中的任何"孩子"(嵌套)行朝右侧缩进。要正确计算NSTextField所需的高度,我需要知道此缩进后NSTextField的宽度。

如何获得此信息?


我应该注意,不可能致电

[outlineView viewAtColumn:0
                      row:[outlineView rowForItem:item]
          makeIfNecessary:YES]

outlineView:heightOfRowByItem:中,这意味着您无法获得对当前单元格的引用。

使用 -[NSOutlineView indentationPerLevel]计算减少的子宽度。

最新更新