Has anybody found a clear, concise example or guide on how to implement a source list using the view-based NSOutlineView introduced in Lion? I\'ve looked at Apple\'s example pro
Take a look at this example.
SideBarDemo
You tagged this with the cocoa-bindings tag, so I assume you mean "with bindings." I whipped up a quick example. Start from a new non-document-based Cocoa Application template in Xcode. Call it whatever you like. First I added some code to make some fake data to bind to. Here's what my AppDelegate header looks like:
#import <Cocoa/Cocoa.h>
@interface SOAppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow *window;
@property (retain) id dataModel;
@end
And here's what my AppDelegate implementation looks like:
#import "SOAppDelegate.h"
@implementation SOAppDelegate
@synthesize window = _window;
@synthesize dataModel = _dataModel;
- (void)dealloc
{
[_dataModel release];
[super dealloc];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
// Make some fake data for our source list.
NSMutableDictionary* item1 = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"Item 1", @"itemName", [NSMutableArray array], @"children", nil];
NSMutableDictionary* item2 = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"Item 2", @"itemName", [NSMutableArray array], @"children", nil];
NSMutableDictionary* item2_1 = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"Item 2.1", @"itemName", [NSMutableArray array], @"children", nil];
NSMutableDictionary* item2_2 = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"Item 2.2", @"itemName", [NSMutableArray array], @"children", nil];
NSMutableDictionary* item2_2_1 = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"Item 2.2.1", @"itemName", [NSMutableArray array], @"children", nil];
NSMutableDictionary* item2_2_2 = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"Item 2.2.2", @"itemName", [NSMutableArray array], @"children", nil];
NSMutableDictionary* item3 = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"Item 3", @"itemName", [NSMutableArray array], @"children", nil];
[[item2_2 objectForKey: @"children"] addObject: item2_2_1];
[[item2_2 objectForKey: @"children"] addObject: item2_2_2];
[[item2 objectForKey: @"children"] addObject: item2_1];
[[item2 objectForKey: @"children"] addObject: item2_2];
NSMutableArray* dataModel = [NSMutableArray array];
[dataModel addObject: item1];
[dataModel addObject: item2];
[dataModel addObject: item3];
self.dataModel = dataModel;
}
@end
There's no particular significance to the fake data structure I created, I just wanted to show something with a couple of sub-levels, etc. The only thing that matters is that the key paths you specify in the bindings in Interface Builder line up with the keys in your data (fake data in this case.)
Then select the MainMenu.xib
file. In the IB editor, do the following steps:
.xib
.children
(for this example; For your data, this should be whatever returns the array of child objects.)dataModel
.xib
.View Based
1
Source List
Static Text - Table View Cell
.Table Cell View
, Model Key Path: objectValue.itemName
(I've used itemName
in the fake data, you would want to use whichever key corresponded to the name of your data items)Save. Run. You should see a source list, and once you've expanded the nodes with children, you might see something like this:
If you're in the Apple Developer Program, you should be able to access the WWDC 2011 Videos. There's one specifically dedicated to working with View-based NSTableView (and NSOutlineView) and it includes pretty thorough coverage of bindings.
Hope that helps!