OSX Dev Day 9 - NSOutlineView
step 1
1.add sourcelist to default view controller in storyboard.
2.add tree controller.
3.add object and change [Custon Class] to 'NSMutableArray' and change [Document lable] to 'playlists'
4.drag from the tree controller to the object.(content)
5.change [Key Paths]'children to 'children' and Leaf to 'isLeaf',and change [Class Name] to 'Playlist'.
6.create 'playlists' class and add two prooerty and one method:
@property NSString *name; @property NSString *creator; - (bool) isLeaf;
- (id) init{ _name= @"New Player"; _creator = @"N/A"; return self; } - (bool) isLeaf { return YES; }
7.Table Column of Outline View bind to 'Tree Controller' and HEADER cell view bind to 'Table cell view',change ([Model Key Path])objectValue to objectValue.name
8.Table View Cell bind to 'Table cell view' and change 'objectvalue' to objectValue.name
9.Drag from the outline view and tree controller to the 'ViewController.h'.
10.add some code:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self addData]; } - (void) addData{ NSDictionary *root = @{@"name":@"Library",@"isLeaf":@NO}; NSMutableArray *array =[[NSMutableArray alloc] initWithCapacity:(NSUInteger)10]; [array addObject:[[PlayList alloc] init]]; [array addObject:[[PlayList alloc] init]]; NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:root]; [dict setObject:array forKey:@"children"]; [_treeController addObject:dict]; }
11.link Outline View's dataSource and delegate to View Controller.
12.add protocol <NSOutlineViewDataSource,NSOutLineViewDelegate> and implem method:
- (bool) isHeader:(id)Item{ Item = (NSTreeNode*)Item; if(Item){ return ![[Item representedObject] isKindOfClass:[PlayLists class]]; } return ![Item isKindOfClass:[PlayLists class]]; } - (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item{ if([self isHeader:item]){ return [outlineView makeViewWithIdentifier:@"HeaderCell" owner:self]; } return [outlineView makeViewWithIdentifier:@"DataCell" owner:self]; }
Step 2