MAC Tree 实现的方法(NSOutlineView) 2 (实现了简单的选择和动态修改不同的数据)
2014-09-24 17:33
1041 查看
MAC Tree 实现的方法(NSOutlineView) 2 (实现了简单的选择和动态修改不同的数据)
在MAC Tree 实现的方法(NSOutlineView) 1 的基础上增加如下
协议增加为:
<NSApplicationDelegate,
NSOutlineViewDelegate, NSOutlineViewDataSource,
NSMenuDelegate>
需要注意的事情是:控件的Delegate需要和类绑定,之前我们已经绑定了DataSource,都绑定同一个类
主要是为了实现选择功能:
// 这个函数必须让控件的数据Delegate和此类绑定(即不光控件的数据源和该类绑定,还要将控件Delegate和该类绑定,才会响应这个函数)
- (void)outlineViewSelectionDidChange:(NSNotification *)notification
{
if ([_outline_view_1
selectedRow] != -1)
{
NSString *item = [_outline_view_1
itemAtRow:[_outline_view_1
selectedRow]];
_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 = nil;
objectValue = item;
return objectValue;
}
// 这个函数必须让控件的数据Delegate和此类绑定(即不光控件的数据源和该类绑定,还要将控件Delegate和该类绑定,才会响应这个函数)
- (void)outlineViewSelectionDidChange:(NSNotification *)notification
{
if ([_outline_view_1 selectedRow] != -1)
{
NSString *item = [_outline_view_1 itemAtRow:[_outline_view_1 selectedRow]];
_Lable1.stringValue=item;
}
}
@end
实现结果:
点Refresh1时,会显示一些数据
点Refresh2时,会显示another数据
如图:
测试成功!
以上可以满足:当操作的时候,不需要小图片,然后数据增加删除是通过全部刷新来实现完成。
2014-9-24 总结 BY DMD
在MAC Tree 实现的方法(NSOutlineView) 1 的基础上增加如下
协议增加为:
<NSApplicationDelegate,
NSOutlineViewDelegate, NSOutlineViewDataSource,
NSMenuDelegate>
需要注意的事情是:控件的Delegate需要和类绑定,之前我们已经绑定了DataSource,都绑定同一个类
主要是为了实现选择功能:
// 这个函数必须让控件的数据Delegate和此类绑定(即不光控件的数据源和该类绑定,还要将控件Delegate和该类绑定,才会响应这个函数)
- (void)outlineViewSelectionDidChange:(NSNotification *)notification
{
if ([_outline_view_1
selectedRow] != -1)
{
NSString *item = [_outline_view_1
itemAtRow:[_outline_view_1
selectedRow]];
_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 = nil;
objectValue = item;
return objectValue;
}
// 这个函数必须让控件的数据Delegate和此类绑定(即不光控件的数据源和该类绑定,还要将控件Delegate和该类绑定,才会响应这个函数)
- (void)outlineViewSelectionDidChange:(NSNotification *)notification
{
if ([_outline_view_1 selectedRow] != -1)
{
NSString *item = [_outline_view_1 itemAtRow:[_outline_view_1 selectedRow]];
_Lable1.stringValue=item;
}
}
@end
实现结果:
点Refresh1时,会显示一些数据
点Refresh2时,会显示another数据
如图:
测试成功!
以上可以满足:当操作的时候,不需要小图片,然后数据增加删除是通过全部刷新来实现完成。
2014-9-24 总结 BY DMD
相关文章推荐
- MAC Tree 实现的方法(NSOutlineView) 1
- 关于dorado框架datatable处的右键菜单无法根据具体数据实现动态分别显示不同菜单的解决方法
- javascript实现Table间隔色以及选择高亮(和动态切换数据)的方法
- asp.net Mvc中实现在View页面中嵌入公用动态数据,实现方法为:include(包含动态页面数据)
- Android编程简单实现ImageView点击时背景图修改的方法
- javascript实现Table间隔色以及选择高亮(和动态切换数据)的方法
- 如何解决FormView中实现DropDownList连动选择时出现 "Eval()、XPath() 和 Bind() 这类数据绑定方法只能在数据绑定控件的上下文中使用" 的错误
- 如何解决FormView中实现DropDownList连动选择时出现 "Eval()、XPath() 和 Bind() 这类数据绑定方法只能在数据绑定控件的上下文中使用" 的错误
- 验证用户是否修改过页面的数据的实现方法
- 主流数据库之间对SQL:2003标准的不同实现方法比较(第四部分 查询结果集中间n行数据)
- Function ALV 简单实现HTML抬头的方法 网上转载的稍微加了一点修改内容
- asp.net2.0实现主从数据表的简单方法
- PHP实现对文本数据库的数据显示、加入、修改、删除、查询五大基本操作的方法
- asp.net2.0实现主从数据表的简单方法
- 用Nhibernate怎么实现数据的添加、删除、修改简单程序
- asp.net ext treepanel 动态加载XML的实现方法
- ajax+jquery+flea+smarty实现了通过选择下拉列表动态显示相应的数据
- 用Nhibernate怎么实现数据的添加、删除、修改简单程序
- 用简单的方法实现Bingo随机出号动态效果
- 验证用户是否修改过页面的数据的实现方法