MAC Tree 实现的方法(NSOutlineView) 1

本文介绍了在MAC上使用NSOutlineView控件创建Tree的步骤,包括在xib中放置控件、绑定DataSource和Delegate,以及实现相关协议方法。示例展示了如何创建一个教师为根节点,包含三个班级的静态数据树形结构。源代码包括H和M文件,目前仅实现固定数据的展示,后期需完善动态更新功能。注意控件类型应选为Cell Based以正常显示数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 = nil;
    objectValue = item;
    return objectValue;
}

@end


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

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

以上2014-9-24 BY DMD总结


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值