NSOutlineView是Cocoa的树形控件,这里介绍它的简单使用。
首先在Xib文件里面拖出控件,然后建立一个委托,把DataSourceDelegate和Delegate绑定该委托。并且在该委托中加入必须的函数,这样数据才能导入进去。
以下是代码实现:
1.保存数据的结构:
#import <Foundation/Foundation.h> @interface OutlineItem : NSObject { NSString *name; NSInteger number; NSInteger id_; NSString *description; BOOL isEnable; BOOL isChecked; NSImage* logo; OutlineItem *parent; NSMutableArray *children; //NSTreeNode *treeNode; //1.存储zqdir.可以是一个disk,也可以是一个folder. //2.存储sms和contacts时,它就是一个vector. void* storage; } @property (readonly) NSMutableArray *children; @property (readwrite, copy) NSString *name; @property (readwrite) NSInteger number; @property (readwrite) NSInteger id_; @property (readwrite) BOOL isEnable; //1.暂时没实现功能. @property (readwrite) BOOL isChecked; @property (readwrite,assign) NSImage *logo; @property (readwrite,assign) void* storage; @property (readwrite,assign) NSTreeNode *treeNode; + (OutlineItem *)rootItem; + (OutlineItem *)setRootItem: (OutlineItem *)item; + (OutlineItem *)getItem:(NSInteger)id_; - (void)addChild:(OutlineItem *)item; - (NSString*)getDescription; - (NSInteger)numberOfChildren;// Returns -1 for leaf nodes - (OutlineItem *)childAtIndex:(NSUInteger)n; @end
#import "OutlineItem.h" static OutlineItem *root = nil; @implementation OutlineItem @synthesize name; @synthesize number; @synthesize id_; @synthesize isEnable; @synthesize isChecked; @synthesize logo; @synthesize children; @synthesize storage; -(id)init { [super init]; name = nil; description = nil; isEnable = true; isChecked = true; number = 0; id_ = 0; children = nil; parent = nil; logo = nil; storage = NULL; return self; } - (void)dealloc { if(children != nil) { for(OutlineItem* child in children) { [child release]; } } //1.从父item里移除. if(parent) { [parent.children removeObject:self]; } [name release]; [description release]; [logo release]; [super dealloc]; } + (OutlineItem *)setRootItem: (OutlineItem *)item { OutlineItem *oldItem = root; root = item; return oldItem; } + (OutlineItem *)getItem:(NSInteger)id_ { int number = [root numberOfChildren]; for(int i = 0;i < number; ++i) { OutlineItem* item = [root childAtIndex:i]; if(item.id_ == id_) { return item; } } return nil; } + (OutlineItem *)rootItem { return root; } - (void)addChild:(OutlineItem *)item { if(children == nil) { children = [NSMutableArray new]; } [children addObject:item]; } - (NSString*)getDescription { if(number) { return [NSString stringWithFormat:@"%@(%d)",name,number]; }else { return name; } } - (NSInteger)numberOfChildren { if(children == nil) { return -1; }else { return [children count]; } } - (OutlineItem *)childAtIndex:(NSUInteger)n { return [children objectAtIndex:n]; } //-(void)addNode:(OutlineItem *)item //{ // [treeNode insertValue:item inPropertyWithKey:@"node"]; //} @end
#import <Foundation/Foundation.h> @class YepOutlineView; @interface OutlineViewDelegate : NSObject<NSOutlineViewDataSource,NSOutlineViewDelegate> { IBOutlet YepOutlineView *outlineView; } - (void)onChickTreeCheckBox:(id) data; - (void)reloadOutlineItem:(id)data; - (void)reloadOneOutlineItem:(id)item; - (void)refreshView; @end
#import "OutlineViewDelegate.h" #import "OutlineItem.h" #import "CheckImageCell.h" #import "YepButtonCell.h" @implementation OutlineViewDelegate - (void)refreshView { [outlineView setNeedsDisplay:YES]; } - (void)reloadOneOutlineItem:(id)item { NSIndexSet *selected = [outlineView selectedRowIndexes]; //1.reloadItem会随机改变select行,所以在调用前先备份,之后还原. [outlineView reloadItem:item reloadChildren:YES]; [outlineView selectRowIndexes:selected byExtendingSelection:NO]; } - (void)reloadOutlineItem:(id)data { [outlineView expandItem:[OutlineItem rootItem]]; //1.直接显示内容. } //delegate - (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item { if(item == nil) { return NO; } return YES; } - (void)onChickTreeCheckBox:(id) data { NSLog(@"onChickTreeCheckBox"); } //dataSource - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item { return (item == nil) ? [OutlineItem rootItem] : [(OutlineItem *)item childAtIndex:index]; //return nil; } - (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item { NSInteger number = (item == nil) ? 1 : [(OutlineItem*)item numberOfChildren]; return number; } //- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item //{ //// OutlineItem* root = [OutlineItem rootItem]; //// NSString * a = [root getDescription]; //// NSString * b = [(OutlineItem*)item getDescription]; //// return (item == nil) ? [root getDescription] : [(OutlineItem*)item getDescription]; // return nil; //} - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item { return (item == nil) ? YES : ([item numberOfChildren] != -1); } - (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item { if(item == nil) return; NSString *test = [tableColumn identifier]; if ([test isEqualToString:@"text"]) { NSTextFieldCell * cell2 = (NSTextFieldCell*)cell; NSFont * Font = nil; // [Font setValue:1]; [cell2 setFont:Font]; [cell2 setStringValue:@"123"]; return; } if ([test isEqualToString:@"image"]) { NSImageCell *cell3 = (NSImageCell *)cell; NSImage* image = [[NSImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"appcaches-normal" ofType:@"png"]]; [cell3 setImage:image]; } if ([test isEqualToString:@"name"]) { CheckImageCell *checkCell = (CheckImageCell*)cell; OutlineItem* outlineItem = (OutlineItem*)item; [checkCell setItem:outlineItem]; [checkCell setState:[outlineItem isChecked]]; [checkCell setTitle:[item getDescription]]; [checkCell setEnabled:[item isEnable]]; } if ([test isEqualToString:@"detail"]){ YepButtonCell *buttonCell = (YepButtonCell*)cell; OutlineItem* outlineItem = (OutlineItem*)item; [buttonCell setStringValue:@"hehe"]; } } @end
[outlineView setHeaderView:nil]; [outlineView setIntercellSpacing: NSMakeSize(5, 5)]; OutlineItem *root = [OutlineItem new]; NSImage* folderImage = [[NSImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"appcaches-normal" ofType:@"png"]]; [root setLogo:folderImage]; [root setId_:1]; [root setIsChecked:NO]; [root setName:@"App Caches"]; [root setIsEnable:YES]; // NSTreeNode *node = [NSTreeNode treeNodeWithRepresentedObject:root]; [OutlineItem setRootItem:root]; OutlineItem *file1 = [OutlineItem new]; NSImage* fileImage = [[NSImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"appcookies-normal" ofType:@"png"]]; [file1 setLogo:fileImage]; [file1 setId_:2]; [file1 setIsChecked:NO]; [file1 setName:@"QQ"]; [file1 setIsEnable:YES]; [root addChild:file1]; OutlineItem *file2 = [OutlineItem new]; fileImage = [[NSImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"appcrashlogs-normal" ofType:@"png"]]; [file2 setLogo:fileImage]; [file2 setId_:3]; [file2 setIsChecked:NO]; [file2 setName:@"Weibo"]; [file2 setIsEnable:YES]; [root addChild:file2]; [file1 addChild:file2]; [outlineView reloadData]; // [outlineView expandItem:root