Search code examples
objective-ccocoansoutlineview

NSOutlineView: Cells are initially too narrow


I did a little demo to isolate a problem I faced in a Project.

When I start the Application, the Cells of the NSOutlineView are too narrow for the text:

Initial View

Then I resize the window with the mouse, making it even narrower than the contents of the NSOutlineView:

Narrowed View

When I now enlarge the window again, the problem is cured. From now on the outline works as expected:

Re-Enlarged View

This is the main method of my AppDelegate:

- (void)applicationDidFinishLaunching:(NSNotification *)notification {
    NSRect frame = NSMakeRect(0., 0., 400., 300.);
    NSUInteger styleMask = NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask;
    _mainWindow = [[NSWindow alloc] initWithContentRect:frame styleMask:styleMask backing:NSBackingStoreBuffered defer:NO];
    _mainWindow.title = @"Outline";

    NSScrollView *leftScrollView = [[NSScrollView alloc] init];
    leftScrollView.hasVerticalScroller = YES;
    leftScrollView.hasHorizontalScroller = NO;
    leftScrollView.drawsBackground = NO;
    leftScrollView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
    _mainWindow.contentView = leftScrollView;

    NSOutlineView *outlineView = [[NSOutlineView alloc] init];
    NSTableColumn *outlineColumn = [[NSTableColumn alloc] initWithIdentifier:@"Menu Item"];
    [outlineView addTableColumn:outlineColumn];
    outlineView.outlineTableColumn = outlineColumn;
    outlineView.selectionHighlightStyle = NSTableViewSelectionHighlightStyleSourceList;
    outlineView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
    outlineView.headerView = nil;
    _outlineDataSourceAndDelegate = [[MROutlineDataSourceAndDelegate alloc] init];
    outlineView.dataSource = _outlineDataSourceAndDelegate;
    outlineView.delegate = _outlineDataSourceAndDelegate;
    leftScrollView.documentView = outlineView;

    [_mainWindow makeKeyAndOrderFront:NSApp];
}

Can anyone please explain that odd behavior?


Solution

  • The answer above was not clean enough for me, so I continued playing around. Now I am pleased: I added a listener to the scroll view that resizes the width of the outline view to be the same as the scroll view. No more need to set the width of the outline column in advance. This is important, if the width is not known in advance.