如何在NSOutlineView + NSTreeController中获取所选节点的完整路径

时间:2014-07-15 09:52:46

标签: objective-c macos nsoutlineview nstreecontroller

enter image description here

我已经从link引用了NSOutlineview。我根据选定的值动态加载所有值。在这里,我在获取路径时构建了一个问题。

喜欢,文件夹路径。 (例如:D:/ NewFolder / Test / blah / blah1

同样我需要从父级获取完整路径选择的子级。在图像中,我选择了 项目。所以我需要的路径应该如下所示,

/Test/New Folder/Untitled/Project

我的代码是

  - (IBAction) ButtonClick:(id)sender
  {
    self.treeoutlineview.delegate = self;
            self.treeoutlineview.dataSource = self;

                NSString *roots = @"/";
                NSIndexPath *indexPath = nil;
                indexPath = [NSIndexPath indexPathWithIndex:[contents count]];
                TreeNode *node = [[TreeNode alloc] init];
                [node TitleSet:roots];
                [self.treeController insertObject:node atArrangedObjectIndexPath:indexPath];
  }
   - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
    childItem = item;
return childItem;
  }

- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
    noofchildren = 0;
return noofchildren;
 }

 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
return YES;
 }
 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
  valueforcolumn = nil; 
 }

- (BOOL)outlineView:(NSOutlineView *)ov shouldSelectItem:(id)item {
{
  rec = @"New Folder|Test|Backup|Project";
   NSArray *arr = [rec componentsSeparatedByString:@"|"];
[loadChildValues removeAllObjects];

NSIndexPath *indexPath = nil;
if (![self.treeController selectionIndexPath])
{
    indexPath = [NSIndexPath indexPathWithIndex:[contents count]];
}
else
{
    if ([[[self.treeController selectedObjects] objectAtIndex:0] isLeaf])
    {
        NSBeep();
        return;
    }

    indexPath = [self.treeController selectionIndexPath];
    indexPath = [indexPath indexPathByAddingIndex:[[[[self.treeController selectedObjects] objectAtIndex:0] children] count]];
}
for (int i = 2; i< [arr count]; i++) {
    [loadChildValues addObject:[arr objectAtIndex:i]];
    TreeNode *node = [[TreeNode alloc] init];
    [node TitleSet:[arr objectAtIndex:i]];
    [self.treeController insertObject:node atArrangedObjectIndexPath:indexPath];
}

}

我是怎么做到的。任何身体都有助于解决这个问题。在此先感谢。

1 个答案:

答案 0 :(得分:1)

我做到了。我做了以下代码。任何人都需要,使用此代码。

在第一步中你需要获得项目的索引路径

- (IBAction)RefreshTree:(id)sender {
    NSIndexPath *indexPath = nil;
    if (![self.treeController selectionIndexPath]) {
        indexPath = [NSIndexPath indexPathWithIndex:[contents count]];
    }
    else {
        if ([[[self.treeController selectedObjects] objectAtIndex:0] isLeaf]) {
            NSBeep();
            NSIndexPath *selectedfiler_indexPath = [[[self.treeController selectedNodes] objectAtIndex:0] indexPath];
            [self selectValue:selectedfiler_indexPath];
        }
        else
        {
            indexPath = [self.treeController selectionIndexPath];
            [self selectValue:indexPath];
        }
    }
}

- (void) selectValue : (NSIndexPath *) indexpath
{
     NSMutableArray *dummyval = [[NSMutableArray alloc] init];
     NSTreeNode *firstSelectedNode = [[self.treeController selectedNodes] objectAtIndex:0];
     NSString *getsel = [firstSelectedNode representedObject];
    [dummyval addObject:getsel];
    int i = 0;
    NSInteger j = [[firstSelectedNode indexPath]length];
    while (i < j) {
        firstSelectedNode = [firstSelectedNode parentNode];
        if (i < j -1)
        {
            NSString *st = [firstSelectedNode representedObject];
            [dummyval addObject:[NSString stringWithFormat:@"%@",st]];
        }
        i++;
    }

    NSArray *reversedArray = [[dummyval reverseObjectEnumerator] allObjects];
    NSString *ret_path=[reversedArray componentsJoinedByString:@"/"];
    ret_path = [ret_path substringWithRange:NSMakeRange(1, [ret_path length] - 1)];
    NSLog("%@",ret_path);
}

输出为:测试/新文件夹/无标题/项目