制作 NSOutlineView 的最简单或推荐的方法是什么?缩进多列?默认情况下,它只缩进大纲列;据我所知,没有内置支持使其缩进其他列。
我有一个 NSOutlineView,它显示两组分层数据之间的比较。为了视觉吸引力,如果大纲列中的某些项目缩进,我想将另一列中同一行的项目缩进相同的缩进量。 (还有第三列显示比较两个项目的结果,该列永远不应该缩进。)
这只能通过子类化 NSOutlineView 来实现吗?子类中需要重写什么?或者有没有更简单的方法让它缩进多列?
最佳答案
事实证明比我想象的要容易。这是解决方案的草图。要缩进 NSOutlineView 中大纲列以外的列,您可以:
- 创建您将用于该列的 NSCell 类的子类,例如 MYIndentedCell
- 向 MYIndentedCell 添加实例变量
indentation
,并为其提供访问器和修改器方法 - 至少将 MYIndentedCell 中的 drawWithFrame:inView: 重写为:
- (void) drawWithFrame: (NSRect) frame inView: (NSView*) view { NSRect newFrame = frame; newFrame.origin.x += indentation; newFrame.size.width -= indentation; [super drawWithFrame: newFrame inView: view]; }
- 如果该列可编辑,您还需要重写
editWithFrame:inView
和selectWithFrame:inView:
,与上面类似 - 将 cellSize 覆盖为:
- (NSSize) cellSize { NSSize cellSize = [super cellSize]; cellSize.width += indentation; return cellSize; }
- 最后,让列中的缩进遵循 NSOutlineView 的大纲列的缩进将由大纲 View 的委托(delegate)处理。委托(delegate)人需要实现以下内容:
- (void) outlineView: (NSOutlineView *) view willDisplayCell: (id) cell forTableColumn: (NSTableColumn *) column item: (id) item { if (column == theColumnToBeIndented) { [cell setIndentation: [view indentationPerLevel] * [view levelForItem: item]]; } }
如果您仍然无法使其正常工作,您可能需要查看 Apple SourceView sample code 中的 ImageAndTextCell.m
我发现这对于弄清楚如何执行上述操作非常有帮助。
关于cocoa - 如何让 NSOutlineView 缩进多列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2419945/