NSOutlineView: remove disclosure triangle and indent

indragie picture indragie · Nov 23, 2010 · Viewed 9.4k times · Source

I'm using NSOutlineView for a project, and can't seem to figure out two things:

  • How to remove the disclosure triangle for tree nodes. Apps like iTunes seem to be able to do this:

alt text

Is there some sort of NSOutlineView Delegate method that is used for this? Or does it require a subclass?

  • How to disable indenting for items. I've tried using setIndentationPerLevel: and setting it to 0, as well as changing the column indent to 0 in Interface Builder, but it does not seem to have any effect.

Answer

Marcel Hansemann picture Marcel Hansemann · Nov 23, 2010

You've run into the right person here. I've had to grapple with this just a week ago.

Removing the disclosure triangle: implement the frameOfOutlineCellAtRow: method in your NSOutlineView subclass and return NSZeroRect (only if you want to hide that particular row's triangle, of course.)

- (NSRect)frameOfOutlineCellAtRow:(NSInteger)row {
    return NSZeroRect;
}

Disable indenting: the outline view's standard layout reserves space at the far left to draw the triangles in, in case the item is expandable. But you can override that for individual items by specifying a different drawing frame. You also do that in your subclass, by responding to this message:

- (NSRect)frameOfCellAtColumn:(NSInteger)column row:(NSInteger)row {
    NSRect superFrame = [super frameOfCellAtColumn:column row:row];


    if ((column == 0) /* && isGroupRow */) {
        return NSMakeRect(0, superFrame.origin.y, [self bounds].size.width, superFrame.size.height);
    }
    return superFrame;
}