首页 > 代码库 > MAC Tree 实现的方法(NSOutlineView) 1

MAC Tree 实现的方法(NSOutlineView) 1

MAC Tree 实现的方法(NSOutlineView) 1 

1.将NSOutlineView控件放在xib界面上

2.增加一个类,让这个类通过控件delegate和控件NSOutlineView绑定DataSource,这样这个表格就会显示这个类所指定的数据。

在这类里面需要在里面增加协议

<NSOutlineViewDataSource>

然后增加必须写的几个函数:

#pragma mark -

#pragma mark ***** Required Methods (unless bindings is used) *****


- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item;

- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item;

- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item;


- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item;


实现结果如图:


一个Teacher表示根目录

三个班级,表示老师教了三个班级

以下是源代码:

H代码

//  TreeGroup_DS.h
//  Created by DMD on 24/9/14.

#import <Foundation/Foundation.h>

@interface TreeGroup_DS : NSObject <NSOutlineViewDataSource>
{
    //For NSoutlineView Level 1 Items
    NSArray *_topLevelItems;
    
    //For NSoutlineView child items
    NSMutableDictionary *_childrenDictionary;
    
    // Control NSOutlineView
    IBOutlet NSOutlineView *_outline_view_1;
}
@end


M代码

//
//  TreeGroup_DS.m
//  Created by DMD on 24/9/14.

#import "TreeGroup_DS.h"

static NSString *root_name =@"刘老师";

@implementation TreeGroup_DS

- (IBAction)OnClick_BT_Refresh:(id)sender
{
    // Group Names
    _topLevelItems = [[NSArray arrayWithObjects:root_name,nil] retain];
    _childrenDictionary = [NSMutableDictionary new];
    
    // Group1 Children
    [_childrenDictionary setObject:[NSArray arrayWithObjects:@"四年级1班", @"二年级5班",@"一年级1班",nil] forKey:root_name];
    
    [_outline_view_1 sizeLastColumnToFit];
    [_outline_view_1 reloadData];
    [_outline_view_1 setFloatsGroupRows:YES];
    [_outline_view_1 setRowSizeStyle:NSTableViewRowSizeStyleDefault];

    // Expand all the root items; disable the expansion animation that normally happens
    [NSAnimationContext beginGrouping];
    [[NSAnimationContext currentContext] setDuration:0];
    [_outline_view_1 expandItem:nil expandChildren:YES];
    [NSAnimationContext endGrouping];
}

- (void)dealloc
{
    [_topLevelItems release];
    [_childrenDictionary release];
    [super dealloc];
}

// Must be function
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
{
    return [[self _childrenForItem:item] objectAtIndex:index];
}

- (NSArray *)_childrenForItem:(id)item
{
    NSArray *children;
    if (item == nil)
    {
        children = _topLevelItems;
    }
    else
    {
        children = [_childrenDictionary objectForKey:item];
    }
    return children;
}

// Must be function
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
{
    if ([outlineView parentForItem:item] == nil)
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

// Must be function
- (NSInteger) outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
{
    return [[self _childrenForItem:item] count];
}

// Must be to show item name
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
{
    id objectValue = http://www.mamicode.com/nil;>

总结:这是一个一个最简单的固定数据的Tree,对Tree没有做动态变化,还需要完善代码。只是初步了解Tree。

注意要控件选择:Cell Base,否则不会显示数据

以上2014-9-24 BY DMD总结


MAC Tree 实现的方法(NSOutlineView) 1