Question

TLDR: I want to implement the method of this question (or any other relevant method) on a NSOutlineView.

A NSOutlineView is more complicated than a NSTableView because some cells may be indented with respect to others, due to parent/child relationships.

I am open to using any method, including AutoLayout if that is possible. My views are simple NSTableCellView instances with only a NSTextField.


I need to be able to set the height of a row in a view-based NSOutlineView depending on the text in that row's NSTextField.

I am currently using the following (simplified) code, which makes use of NSString+Geometics to calculate the approximate height of the view given its width:

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

The idea behind this code is to keep a scratch cell around in order to make use of its default bounds property. This works correctly for the top level row.

However, any 'child' (nested) rows in the NSOutlineView are indented towards the right. To calculate the required height of the NSTextField correctly, I need to know the width of the NSTextField after this indentation.

How can I obtain this information?


I should note that it is not possible to call

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

from within outlineView:heightOfRowByItem:, which means that you can't get a reference to the current cell.

Was it helpful?

Solution

Use -[NSOutlineView indentationPerLevel] to calculate reduced child width.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top