始终在 NSOutlineView 中显示显示三角形,即使对于空条目也是如此

Always show disclosure triangle in NSOutlineView even for empty entries

我有一个应用程序在 NSOutlineView 中显示 "chapters and pages"。

为了让用户明白 "chapters" 可以包含 "pages",我希望大纲视图始终显示章节的显示三角形,即使它们中没有页面(即为空)。

这是一个例子:

请注意第一章是空的(无页)并且没有 披露三角形。

我希望显示三角形,这样当用户单击它时,他们会意识到这是一个可以包含其他项目的 "container" 项目。

我尝试实现各种委托方法,但无法让显示三角形显示空条目。

示例项目:https://www.dropbox.com/s/fdggjod78t0isjr/OutlineViewTest.zip?dl=0

好的,我明白了。您正在使用 Cocoa 绑定,它假定某些 dataSource 方法不应被调用。如显示在 NSOutlineViewDataSource reference page

'- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item'

IMPORTANT

While this method is marked as @optional in the protocol, you must implement this method if you are not providing the data for the outline view using Cocoa bindings.

尝试在没有 Cocoa 绑定的情况下实现它。或者你可以找到破解绑定过程的方法,看看如何拒绝披露按钮删除。

是的,还有第三种方法。您可以创建 MyTableCellView.xib 并添加您自己的披露按钮。

UPD

要确定哪个项目应该有显示三角形,在 Tree Controller 属性检查器选项卡中有一个 Leaf 关键路径。

不要忘记像这样将模型的值添加到您的数据中

@{@"title": @"Chapter 1 (empty)", @"isLeaf":@(NO)}

您需要实施 NSOutlineViewDelegate outlineView:shouldShowOutlineCellForItem: 并拥有它 return 是。

- (BOOL)outlineView:(NSOutlineView *)outlineView shouldShowOutlineCellForItem:(id)item {
    return YES;
}

"outline cell" 是披露三角形。除了 return 之外,您还需要此 isExpandable

method's reference 说:

Returning NO causes frameOfOutlineCellAtRow: to return NSZeroRect

the 1st answer here about how to change the triangle image 似乎表明 frame 用于单元格和基于视图的大纲视图,因此此方法应该适用于两者。 (请注意,更简单的解决方案是那里的第二个答案。)

这个答案是 wiki...我怀疑我错过了什么。