[Cocoa]_[NSOutlineView]_[多根显示]

   总结下Cocoa下NSOutlineView的使用,要想搞明白NSOutlineView的使用,就要搞懂NSOutlineView的协议的函数的作用,下面结合例子讲解:

   1.首先,我们先拖出表格到窗口上,然后给表格的dataSource和delegate绑定一个委托,我们这里直接绑定在AppDelegate上。

 [Cocoa]_[NSOutlineView]_[多根显示]_第1张图片

2.然后定义一个数据结构来保存每一行的数据,实现如下:

outlineviewData.h

#import <Foundation/Foundation.h>

@interface outlineviewData : NSObject
{
    NSString *name;
    NSString *iD;
    NSMutableArray *array;
    
}

@property (readwrite,copy) NSString *name;
@property (readwrite,copy) NSString *iD;
@property (readwrite,copy) NSMutableArray *array;

@end

outlineviewData.m

#import "outlineviewData.h"



@implementation outlineviewData

@synthesize name;
@synthesize iD;
@synthesize array;

-(id)init
{
    name = nil;
    iD = nil;
    array = [NSMutableArray new];
    
    return self;
}

@end

3.在AppDelegate里面实现NSOutlineView的委托和数据的初始化,实现如下:

AppDelegate.h

#import <Cocoa/Cocoa.h>
@class outlineviewData;

@interface AppDelegate : NSObject <NSApplicationDelegate>
{
    IBOutlet NSOutlineView *outlineView;
    outlineviewData *root;
}

@property (assign) IBOutlet NSWindow *window;

@end

AppDelegate.m

#import "AppDelegate.h"
#import "outlineviewData.h"

@implementation AppDelegate

-(void)awakeFromNib
{
   // [outlineView setHeaderView:nil];
    
    root = [[outlineviewData alloc] init];
    
    outlineviewData *data = [[outlineviewData alloc] init];
    [data setName:@"小泉纯一郎"];
    [data setID:@"123"];
    [[root array] addObject:data];
    
    outlineviewData *child = [[outlineviewData alloc] init];
    [child setName:@"普京"];
    [child setID:@"111"];
    [[data array] addObject:child];
    
    outlineviewData *data2 = [[outlineviewData alloc] init];
    [data2 setName:@"江泽民"];
    [data2 setID:@"456"];
    [[root array] addObject:data2];
    
    outlineviewData *child2 = [[outlineviewData alloc] init];
    [child2 setName:@"奥巴马"];
    [child2 setID:@"222"];
    [[data2 array] addObject:child2];
    
    [outlineView reloadData];
}

//-------------------------NSOutlineView protocol----------------------------------

//获取表格数据
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
{
    if (item == nil) {
        return [[root array] objectAtIndex:index];
    }
    else
    {
        return [[(outlineviewData *)item array] objectAtIndex:index];
    }
}

//获取表格数据的个数
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
{
  // return [[root mutableChildNodes] count];
    NSInteger number = (item == nil) ? [[root array] count] : [[(outlineviewData*)item array] count];
    return number;    
}

//判断能否展开
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
{
    outlineviewData *data = (outlineviewData *)item;
    return [[data array] count];
}

//判断是否应该展开
- (BOOL)outlineView:(NSOutlineView *)outlineView shouldExpandItem:(id)item
{
    outlineviewData *data = (outlineviewData *)item;
    return [[data array] count];
}

//显示数据到表格上
- (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item
{
    outlineviewData *data = (outlineviewData *)item;
    NSString *identifier = [tableColumn identifier];
   
    if ([identifier isEqualToString:@"name"])
    {
        NSTextFieldCell * textCell = cell;
        [textCell setTitle:data.name];
    }
    else if([identifier isEqualToString:@"id"])
    {
         NSTextFieldCell * textCell = cell;
        [textCell setTitle:data.iD];
    }
}

@end

显示效果:

[Cocoa]_[NSOutlineView]_[多根显示]_第2张图片


总结:重点是掌握那几个协议的作用。


代码例子下载链接:http://download.csdn.net/detail/yepeng2014/9149975

你可能感兴趣的:([Cocoa]_[NSOutlineView]_[多根显示])