How to use NSOutlineView and NSTreeController?

Discussion in 'Mac Programming' started by mkristain, Aug 20, 2011.

  1. mkristain macrumors regular

    Joined:
    Aug 18, 2011
    #1
    for NSOutlineView i have tried this but it can insert only blank values in outlineview


    Code:
    
    @interface MyDataObject : NSObject 
    {
    	NSMutableDictionary         *root;
    	
    	id parent;
    }
    
    +(id)weakReferenceWithParent:(id)parent;
    -(void)setParent:(id)_parent;
    -(id)parent;
    
    @property (assign,readonly) NSMutableDictionary *root;
    
    @end
    
    //-----------------------------------------------
    #import "MyDataObject.h"
    
    @implementation MyDataObject
    
    @synthesize root;
    
    +(id)weakReferenceWithParent:(id)parent 
    {
        id weakRef=[[[MyDataObject alloc] init] autorelease];
        [weakRef setParent:parent];
        return weakRef;
    }
    -(void)setParent:(id)_parent 
    {
        parent=_parent;
    }
    -(id)parent 
    {
        return parent;
    }
    
    - (id)init
    {
        if ((self = [super init])) 
        {
            root = [NSMutableDictionary new];
        }
        return self;
    }
    
    - (void)dealloc
    {
        [root release];
        [super dealloc];
    }
    
    
    @end
    //-----------------------------------------------
    
    #import "MyDataObject.h"
    
    @interface MyTableController : NSControl {
    
    	NSMutableArray * dataStore;
    	IBOutlet NSOutlineView* OutlineView;
    }
    
    @property (assign) NSMutableArray * dataStore;
    @property (assign) NSOutlineView* OutlineView;
    
    @end
    //-----------------------------------------------
    #import "MyTableController.h"
    
    @implementation MyTableController
    
    @synthesize dataStore;
    @synthesize OutlineView;
    
    
    -(NSMutableDictionary *)groupWithTitle:(id)title {
        NSMutableDictionary *group=[NSMutableDictionary dictionary];
        [group setObject:[NSMutableArray array] forKey:@"CHILDREN"];
        if (title) [group setObject:title forKey:@"TITLE"];
        return group;
    }
    -(NSMutableDictionary *)newItemForGroup:(NSMutableDictionary *)group title:(id)title{
        NSMutableDictionary *item=[NSMutableDictionary dictionary];
        id children=[group objectForKey:@"CHILDREN"];
        [children addObject:item];
        if (title) [item setObject:title forKey:@"TITLE"];
        if (group) [item setObject:[MyDataObject weakReferenceWithParent:group] forKey:@"PARENT"];
        return item;
    }
    
    
    -(BOOL)outlineView:(NSOutlineView *)ov isItemExpandable:(id)item {
        id children;
        if (!item) {
            children=dataStore;
        } else {
            children=[item objectForKey:@"CHILDREN"];
        }
        if ((!children) || ([children count]<1)) return NO;
        return YES;
    }
    -(int)outlineView:(NSOutlineView *)ov numberOfChildrenOfItem:(id)item {
        id children;
        if (!item) {
            children=dataStore;
        } else {
            children=[item objectForKey:@"CHILDREN"];
        }
        return [children count];
    }
    -(id)outlineView:(NSOutlineView *)ov child:(int)index ofItem:(id)item {
        id children;
        if (!item) {
            children=dataStore;
        } else {
            children=[item objectForKey:@"CHILDREN"];
        }
        if ((!children) || ([children count]<=index)) return nil;
        return [children objectAtIndex:index];
    }
    -(id)outlineView:(NSOutlineView *)ov objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
        return [item objectForKey:[tableColumn identifier]];
    }
    
    - (void)awakeFromNib
    {
     self.dataStore=[[NSMutableArray array]  retain];
        id group1=[self groupWithTitle:@"group1"];
        id group2=[self groupWithTitle:@"group2"];
        [self newItemForGroup:group1 title:@"item1"];
        [self newItemForGroup:group2 title:@"item2"];
        
        [self.dataStore addObject:group1];
        [self.dataStore addObject:group2];
        [OutlineView reloadData];
    }
    
    @end
    
    

    Can anybody please help me to use NSOutlineView and NSTreeController in my cocoa application.

    this is my first cocoa application so i dont understand how can i do this.

    please help me foe this.

    thanks .
     
  2. MrFusion, Aug 20, 2011
    Last edited: Aug 20, 2011

    MrFusion macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #2
    From what I can tell, you didn't subclass NSOutlineView or NSTreeController.
    You implemented a dataobject (MyDataObject) and a datasource (MyTableController). The datasource keeps track of the data objects (adding, removing etc).

    You can use an NSOutlineView with a datasource if you implement the datasource protocol, so you tried in MyTableController. Such a datasource doesn't have to be a subclass of NSControl. Can be anything. Only thing you have to is connect the outlet of the NSOutlineView to an instance of your datasource. You can do this in interface builder. Just add an NSOutlineView to your window, double click it until the NSOutlineView is selected (in your inspector). Just selecting it will give you the scrollview that contains the actual outlineview.

    However, you don't have to implement the NSOutlineView datasource. You could use an NSTreeController instead. This will take care of a lot of the details: such as adding and removing data objects. In addition to the NSOutlineView, add an instance of NSTreeController to your nib file. Just drag it the objects window/panel. You can configure it within interface builder. Just provide class name, etc. Connect the datasource of NSOutlineView to the NSTreeController, and the key path of columns and you're set to go.

    You can also use core data to create data classes. Then you set the managedobjectcontext of your nstreecontroller.

    Edit: in fact, with core data, NSOutlineView and NSTreeController, you don't have to write a single line of code. It won't do anything spectacular though like drag and drop or copy and paste.
     

Share This Page