【问题标题】:Expand only root node in NSOutlineView仅展开 NSOutlineView 中的根节点
【发布时间】:2014-11-13 01:50:47
【问题描述】:

我正在尝试解决一个简单的问题,即仅扩展我的 NSOutlineView 中的根项目,但没有运气。我可以展开所有项目和选定项目,但我似乎无法弄清楚如何准确地确定如何仅扩展根节点。大纲视图显示文件夹树,我想显示根文件夹下可用的文件夹,但我不想展开子文件夹。

我目前尝试设置我的 NSTreeController 的 selectionIndexPath,然后在大纲视图中展开选择:

 NSIndexPath *indexPath;
 NSUInteger section = [indexPath indexAtPosition:0];
 NSIndexPath *ip = [NSIndexPath indexPathWithIndex:section];
 [outlineView collapseItem:nil collapseChildren:YES];

 [self.treeController setSelectionIndexPath:ip];
 [outlineView expandItem:[self.treeController selectionIndexPath]];

这不起作用。非常感谢有关如何正确解决此问题的建议。

干杯,特隆德

解决方案: 我已经拥有的内容和@PaulPatterson 建议的以下组合非常有效:

[outlineView collapseItem:nil collapseChildren:YES];
NSIndexPath *indexPath;
NSUInteger section = [indexPath indexAtPosition:0];
NSIndexPath *ip = [NSIndexPath indexPathWithIndex:section];
[outlineView collapseItem:nil collapseChildren:YES];

[self.treeController setSelectionIndexPath:ip];
id node = [[self.treeController selectedNodes] firstObject];
[outlineView expandItem:node];

【问题讨论】:

    标签: objective-c cocoa nsoutlineview nsindexpath nstreecontroller


    【解决方案1】:

    问题是[-NSOutlineView expandItem:] 预期的值与[-NSTreeController selectionIndexPath] 返回的值不匹配吗?

    就目前而言,您的传递是 NSIndexPath 实例到 expandItem:。我很欣赏expandItem: 不知道它期望作为参数的特定类(文档声明它接受任何对象 - id),但我的经验是它需要一个 node 对象 - 包含的对象类型在[-NSTreeController selectedNodes] 返回的数组中。请尝试以下操作:

    if ([[self.treeController selectedNodes] count] == 1) {
        id node = [[self.treeController selectedNodes] firstObject];
        [outlineView expandItem:node];
    }
    

    【讨论】:

    • 感谢您对我的 NSOutlineView 问题的持续帮助!
    猜你喜欢
    • 2010-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-27
    • 1970-01-01
    • 2019-01-07
    • 1970-01-01
    相关资源
    最近更新 更多