如何使NSOutlineView缩进多列?

3
什么是使NSOutlineView缩进多个列的最简单或建议的方法?默认情况下,它仅缩进大纲列;据我所知,没有内置支持使其缩进其他列。
我有一个NSOutlineView,显示两组分层数据之间的比较。为了视觉吸引力,如果大纲列中的某个项目被缩进,我希望通过相同的缩进量缩进另一列中同一行的项目。(还有第三列显示比较两个项目的结果,此列不应缩进。)
这只能通过子类化NSOutlineView来实现吗?在子类中需要覆盖什么?还是有更简单的方法使其缩进多个列?
1个回答

2
原文如下:

结果比我预期的要容易。以下是解决方案的草图。要在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中缩进列的解决方案的说明。
最后,使列中的缩进遵循 NSOutlineView 的大纲列的缩进将由大纲视图的代理处理。代理需要实现以下内容:
     - (void) outlineView: (NSOutlineView *) view
              willDisplayCell: (id) cell
              forTableColumn: (NSTableColumn *) column
              item: (id) item
     {
       if (column == theColumnToBeIndented) {
         [cell setIndentation:
                  [view indentationPerLevel] * [view levelForItem: item]];
       }
     }
如果您仍然无法使其正常工作,您可能需要查看苹果公司的SourceView示例代码中的 ImageAndTextCell.m,我在弄清上述内容时发现它非常有用。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接