Search code examples
objective-cmacosnsoutlineviewnstreecontroller

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


enter image description here

I have referred NSOutlineview from this link. 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

In same I need to get the full path selected child from parent. In the image, i selected 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.


Solution

  • 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

    - (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