首页 > 代码库 > MAC Tree 实现的方法(NSOutlineView) 2 (实现了简单的选择和动态修改不同的数据)

MAC Tree 实现的方法(NSOutlineView) 2 (实现了简单的选择和动态修改不同的数据)

MAC Tree 实现的方法(NSOutlineView) 2 (实现了简单的选择和动态修改不同的数据)

在MAC Tree 实现的方法(NSOutlineView) 1 的基础上增加如下

协议增加为:

<NSApplicationDelegate,NSOutlineViewDelegate, NSOutlineViewDataSource,NSMenuDelegate

需要注意的事情是:控件的Delegate需要和类绑定,之前我们已经绑定了DataSource,都绑定同一个类

主要是为了实现选择功能:

// 这个函数必须让控件的数据Delegate和此类绑定(即不光控件的数据源和该类绑定,还要将控件Delegate该类绑定,才会响应这个函数)

- (void)outlineViewSelectionDidChange:(NSNotification *)notification

{

   if ([_outline_view_1selectedRow] != -1)

    {

        NSString *item = [_outline_view_1itemAtRow:[_outline_view_1selectedRow]];

       _Lable1.stringValue=item;

    }

}

以下是实现代码

H文件

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

#import <Foundation/Foundation.h>

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


M文件

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

//1. 实现动态的方法刷新,更新Tree里面的数据(UNDONE)

#import "TreeGroup_DS.h"

@implementation TreeGroup_DS

- (id)init
{
    if ((self = [super init]))
    {
        [self tree_refresh:20 RootName:@"Teacher Liu(3)"];
        
    }
    return self;
}
- (void)loadView
{

}
- (IBAction)OnClick_BT_Refresh:(id)sender
{
   [self tree_refresh:3 RootName:@"Teacher Liu(3)"];
}

- (IBAction)OnClick_BT_Refresh2:(id)sender
{
    [self tree_refresh:5  RootName:@"Teacher Zhang(5)"];
}

-(void) get_tree_data:(NSInteger)tree_count RootName:(NSString*)item_name
{
    // Group Names
    _topLevelItems = [[NSArray arrayWithObjects:item_name,nil] retain];
    _childrenDictionary = [NSMutableDictionary new];
    
    // Child Names of group
    NSMutableArray *array = [NSMutableArray arrayWithObjects:nil];
    NSString *child_name=nil;
    for (int i=0; i<tree_count; i++)
    {
        child_name = [NSString stringWithFormat:@"Grade(%d)",i+1];
        [array addObject:child_name];
    }
    [_childrenDictionary setObject:array forKey:item_name];
    NSLog(@"dictionary:%@",_childrenDictionary);
}

-(void) tree_refresh:(NSInteger)tree_count RootName:(NSString*)item_name
{
    [self get_tree_data:tree_count RootName:item_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;>

实现结果:

点Refresh1时,会显示一些数据

点Refresh2时,会显示another数据

如图:






测试成功!

以上可以满足:当操作的时候,不需要小图片,然后数据增加删除是通过全部刷新来实现完成。

2014-9-24 总结 BY DMD






MAC Tree 实现的方法(NSOutlineView) 2 (实现了简单的选择和动态修改不同的数据)