简体   繁体   English

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

[英]How to get Full path of selected Node in NSOutlineView + NSTreeController

在此处输入图片说明

I have referred NSOutlineview from this link . 我从此链接引用了NSOutlineview。 I dynamically load all values as based on selected values. 我根据所选值动态加载所有值。 Here i struct a issue while getting paths. 在这里,我在获取路径时构造了一个问题。

Like, Folder path. 像,文件夹路径。 (eg.: D:/NewFolder/Test/blah/blah1 (例如:D:/ NewFolder / Test / blah / blah1

In same I need to get the full path selected child from parent. 同样,我需要从父级中选择子级的完整路径。 In the image, i selected Project. 在图像中,我选择了Project。 so I need the path should be get like as follows, 所以我需要的路径应该如下所示,

/Test/New Folder/Untitled/Project

My code is 我的代码是

  - (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];
}

}

How i do this. 我该怎么做。 Any body pls help to resolve this. 任何机构都可以帮助解决这一问题。 Thanks in Advance. 提前致谢。

I did it. 我做的。 I have did the following code. 我做了以下代码。 any one need, use this code. 任何需要的人,请使用此代码。

In first step u need to get Indexpath of item 第一步,您需要获取商品的Indexpath

- (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);
}

Output is : Test/New Folder/Untitled/Project 输出是:测试/新文件夹/无标题/项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM