Creating NSOutlineView programmatically
Posted in Apple on March 9th, 2010 by Saba – 2 CommentsHi Everyone,
I am trying to create an NSOutlineView programmatically inside of an NSDrawer. I am using the example datasource from /Developer/Examples/ AppKit/OutlineView the “FileSystemItem”. My issue is that although the root item is displayed in the OutlineView, it is not expandable and does not show any of the children. The datasource is identical to the one in the Apple example so I’m guessing my issue is the way I am creating the NSOutlineView in code rather than in IB. The result is that I see an outlineview with one item (the root item) with one table column header and no expand icons. If anyone can see something I’m doing wrong I would very much appreciate the help My code is attached.
Thank you ahead of time! Zack
NSView *contentView = [win contentView]; [contentView setAutoresizesSubviews:YES];
outlineView = [[NSOutlineView alloc] initWithFrame: [contentView frame]];
//DataSource. code is attached below ZBFSDataSource *ds = [[ZBFSDataSource alloc] init]; [outlineView setDataSource: ds];
NSTableColumn *c = [[NSTableColumn alloc] initWithIdentifier: @”NAME”]; [c setEditable: NO]; [c setMinWidth: 150.0]; [outlineView addTableColumn: c]; [c release];
[outlineView reloadData];
scrollView = [[NSScrollView alloc] initWithFrame: [outlineView frame]]; [scrollView setDocumentView: outlineView]; [scrollView setAutoresizesSubviews: YES];
NSDrawer *drawer = [[NSDrawer alloc] initWithContentSize:NSMakeSize(200.0, 400.0) preferredEdge:NSMinXEdge]; [drawer setParentWindow: win]; [drawer setContentView: scrollView]; [drawer open];
DataSource:
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item { return (item == nil) ? 1 : [item numberOfChildren]; }
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable: (id)item { return (item == nil) ? YES : ([item numberOfChildren] != -1); }
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item { return (item == nil) ? [FileSystemItem rootItem] : [(FileSystemItem *)item childAtIndex:index]; }
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item { return (item == nil) ? @”/” : (id)[item relativePath]; }