NSOutlineView:细胞最初太窄了(NSOutlineView: Cells are initially too narrow)

我做了一个小小的演示,以隔离我在项目中遇到的问题。

当我启动应用程序时, NSOutlineView的单元格对于文本来说太窄了:

然后我用鼠标调整窗口大小,使其比NSOutlineView的内容更窄:

当我现在再次放大窗户时,问题就解决了。 从现在开始,大纲按预期工作:

这是我的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]; }

任何人都可以解释这种奇怪的行为?

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:

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

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

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?

最满意答案

我不知道这是否是最优雅的解决方案,但至少它是有效的。 我在创建NSTableColumn后添加了以下单行:

outlineColumn.width = SIDEBAR_WIDTH_DEFAULT;

而已。 花了我差不多一天才弄明白:(

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.

更多推荐