[Cocoa]_[初级]_[使用NSOutlineView创建多根显示]

[Cocoa]_[初级]_[使用NSOutlineView创建多根显示],第1张

概述场景:主要应用于界面布局中可隐藏数据和展开显示数据。 重点:理解MqjOutlineViewDelegate.m文件- (NSTreeNode*) doTreeNodeFromArray:(OutlineViewData *) data 函数如何使用递归进行数据封装, 理解+(id)treeNodeWithRepresentedObject:(id) modelObject;函数是怎么创建树形结构

场景:主要应用于界面布局中可隐藏数据和展开显示数据。

重点:理解MqjOutlineVIEwDelegate.m文件- (NSTreeNode*) doTreeNodeFromArray:(OutlineVIEwData *) data 函数如何使用递归进行数据封装,

理解+(ID)treeNodeWithRepresentedobject:(ID) modelObject;函数是怎么创建树形结构的

具体例子如下:

1.编写代码

OutlineVIEwData.h

#import <Foundation/Foundation.h>@interface OutlineVIEwData : NSObject{    Nsstring *name;    Nsstring *number;       //表格状态标识    BOol isHasChild;    BOol isParentRoot;    NSMutableArray *arrayDatas;}@property (reaDWrite,copy) Nsstring *name;@property (reaDWrite,copy) Nsstring *number;@property (reaDWrite,assign) BOol isHasChild;@property (reaDWrite,assign) BOol isParentRoot;@property (reaDWrite,assign) NSMutableArray *arrayDatas;@end


OutlineVIEwData.m

#import "OutlineVIEwData.h"@implementation OutlineVIEwData@synthesize name;@synthesize number;@synthesize isHasChild;@synthesize isParentRoot;@synthesize arrayDatas;-(ID) init{    self =[super init];    name = nil;    number =@"0";    isHasChild =NO;    isParentRoot =NO;        arrayDatas = [NSMutableArray new];    return self;}@end


SimpleNodeData.h

#import <Cocoa/Cocoa.h>@interface SimpleNodeData : NSObject {//@private    // ivars for the propertIEs declared below    Nsstring *name;    Nsstring *number;        //表格状态标识    BOol isHasChild;    BOol isParentRoot;        BOol expandable;    BOol selectable;    BOol container;}@property(reaDWrite,copy) Nsstring *name;@property(reaDWrite,copy) Nsstring *number;@property(reaDWrite,getter=isExpandable) BOol expandable;@property(reaDWrite,getter=isSelectable) BOol selectable;@property(reaDWrite,getter=isContainer) BOol container;@property(reaDWrite,assign) BOol isHasChild;@property(reaDWrite,assign) BOol isParentRoot;-(ID) initWithTreeData:(ID) data;+ (SimpleNodeData *)nodeDataWithTreeData:(ID)data;@end


SimpleNodeData.m

#import "SimpleNodeData.h"#import "OutlineVIEwData.h"@implementation SimpleNodeData@synthesize name,number,expandable,selectable,container,isHasChild,isParentRoot;- (ID)init {    self = [super init];    name = @"UnTitled";    number =@"0";       expandable = YES;    selectable = YES;    container = YES;    isHasChild =NO;    isParentRoot =NO;    return self;}-(ID) initWithTreeData:(ID) data{    self = [self init];    OutlineVIEwData *ii =data;    name = ii.name;    number =ii.number;       isHasChild =ii.isHasChild;    isParentRoot =ii.isParentRoot;       return self;}+ (SimpleNodeData *)nodeDataWithTreeData:(ID)data{     return [[[SimpleNodeData alloc] initWithTreeData:data] autorelease];}- (voID)dealloc {    [name release];    [number release];    [super dealloc];}@end


MqjOutlineVIEwDelegate.h

#import <Cocoa/Cocoa.h>@interface MqjOutlineVIEwDelegate : NSObject<NSOutlineVIEwDelegate,NSOutlineVIEwDataSource>{    IBOutlet NSTextFIEld *contentLabel;        NSTreeNode *roottreeNode;        IBOutlet NSOutlineVIEw *outlineVIEw;}- (NSArray *)draggednodes;- (NSArray *)selectednodes;-(voID) onChickTreeCheckBox:(ID) data;@end


MqjOutlineVIEwDelegate.m

#import "MqjOutlineVIEwDelegate.h"#import "OutlineVIEwData.h"#import "SimpleNodeData.h"#import "MqjCenterTextFIEldCell.h"@implementation MqjOutlineVIEwDelegate-(voID)awakeFromNib{       OutlineVIEwData *data= [self doloadDataToArray];        roottreeNode =[[self doTreeNodeFromArray:data] retain];    [outlineVIEw setIndentationMarkerFollowsCell:YES];    [outlineVIEw setIgnoresMultiClick:YES];        [outlineVIEw expandItem:roottreeNode expandChildren:YES];    [outlineVIEw reloadData];        //设置子项的展开    //    [outlineVIEw isExpandable:[roottreeNode.mutableChildNodes objectAtIndex:0]];    [outlineVIEw expandItem:[roottreeNode.mutableChildNodes objectAtIndex:0] expandChildren:NO];}// The NSOutlineVIEw uses 'nil' to indicate the root item. We return our root tree node for that case.- (NSArray *)childrenForItem:(ID)item {    if (item == nil) {        return [roottreeNode childNodes];    } else {        return [item childNodes];    }}// required methods.- (ID)outlineVIEw:(NSOutlineVIEw *)outlineVIEw child:(NSInteger)index ofItem:(ID)item {    // 'item' may potentially be nil for the root item.    NSArray *children = [self childrenForItem:item];    // This will return an NSTreeNode with our model object as the representedobject    return [children objectAtIndex:index];}- (BOol)outlineVIEw:(NSOutlineVIEw *)outlineVIEw isItemExpandable:(ID)item {    // 'item' will always be non-nil. It is an NSTreeNode,since those are always the objects we give NSOutlineVIEw. We access our model object from it.    SimpleNodeData *nodeData = [item representedobject];    // We can expand items if the model tells us it is a container    return nodeData.container;}- (NSInteger)outlineVIEw:(NSOutlineVIEw *)outlineVIEw numberOfChildrenOfItem:(ID)item {    // 'item' may potentially be nil for the root item.    NSArray *children = [self childrenForItem:item];        return [children count];}// To get the "group row" look,we implement this method.- (BOol)outlineVIEw:(NSOutlineVIEw *)outlineVIEw isGroupItem:(ID)item {    SimpleNodeData *nodeData = [item representedobject];            return NO;}- (BOol)outlineVIEw:(NSOutlineVIEw *)outlineVIEw shouldExpandItem:(ID)item {    // query our model for the answer to this question    SimpleNodeData *nodeData = [item representedobject];    return nodeData.expandable;}- (voID)outlineVIEw:(NSOutlineVIEw *)outlineVIEw willdisplayCell:(NSCell *)cell fortableColumn:(NStableColumn *)tableColumn item:(ID)item {    SimpleNodeData *nodeData = [item representedobject];        // For all the other columns,we don't do anything.    Nsstring *IDentifIEr =[tableColumn IDentifIEr];            if ([IDentifIEr isEqualToString:@"name"])    {        MqjCenterTextFIEldCell *textCell =(MqjCenterTextFIEldCell*)cell;        [textCell setStringValue:nodeData.name];    }    else if ([IDentifIEr isEqualToString:@"number"])    {        MqjCenterTextFIEldCell *textCell =(MqjCenterTextFIEldCell*)cell;        [textCell setStringValue:nodeData.number];           }       }- (ID)outlineVIEw:(NSOutlineVIEw *)outlineVIEw objectValueFortableColumn:(NStableColumn *)tableColumn byItem:(ID)item{    ID objectValue = nil;    return objectValue;}//表格数据的封装-(OutlineVIEwData*) doloadDataToArray{    OutlineVIEwData *rootData =[OutlineVIEwData new];    [rootData setname:@"root"];    [rootData setIsParentRoot:YES];        for (int i =0; i<5; i++)    {        OutlineVIEwData *ii =[OutlineVIEwData new];        [ii setname:[Nsstring stringWithFormat:@"parent%d",i+1]];        [ii setNumber:@"8888"];        [ii setIsParentRoot:YES];                if (i<3)        {            [ii setIsHasChild:YES];                for (int j =0; j<3; j++)        {            OutlineVIEwData *temp =[OutlineVIEwData new];            [temp setname:[Nsstring stringWithFormat:@"%@-item%d",ii.name,j+1]];            [temp setNumber:@"55"];            [temp setIsHasChild:NO];            [ii.arrayDatas addobject:temp];        }        }        [rootData.arrayDatas addobject:ii];    }    return rootData;}//递归函数填充数据到NSTreeNode对象,构建一个树形目录结构显示多根- (NSTreeNode*) doTreeNodeFromArray:(OutlineVIEwData *) data{    OutlineVIEwData *tempData = data;    NSMutableArray *children =tempData.arrayDatas;            SimpleNodeData *nodeData = [SimpleNodeData nodeDataWithTreeData:tempData];    // The image for the nodeData is lazily filled in,for performance.        // Create a NSTreeNode to wrap our model object. It will hold a cache of things such as the children.    NSTreeNode *result = [NSTreeNode treeNodeWithRepresentedobject:nodeData];        // Walk the dictionary and create NSTreeNodes for each child.            for (OutlineVIEwData * item in children) {        // A particular item can be another dictionary (IE: a container for more children),or a simple string        NSTreeNode *childTreeNode;        //if ([item isKindOfClass:[OutlineVIEwData class]])        if (item.isHasChild)        {            // Recursively create the child tree node and add it as a child of this tree node            childTreeNode = [self doTreeNodeFromArray:item];                               } else {            // It is a regular leaf item with just the name            SimpleNodeData *childNodeData = [[SimpleNodeData alloc] initWithTreeData:item];            childNodeData.container = NO;            childTreeNode = [NSTreeNode treeNodeWithRepresentedobject:childNodeData];            [childNodeData release];        }        // Now add the child to this parent tree node        [[result mutableChildNodes] addobject:childTreeNode];    }    return result;}- (NSArray *)draggednodes {    return nil;}- (NSArray *)selectednodes {    return [self selectedItems];}- (NSArray *)selectedItems {    NSMutableArray *items = [NSMutableArray array];    NSIndexSet *selectedRows = [outlineVIEw selectedRowIndexes];    if (selectedRows != nil) {        for (NSInteger row = [selectedRows firstIndex]; row != NSNotFound; row = [selectedRows indexGreaterThanIndex:row]) {            [items addobject:[outlineVIEw itemAtRow:row]];        }    }    return items;}- (voID)outlineVIEwSelectionDIDChange:(NSNotification *)notification{    NSLog(@"click tree");    NSArray *selectednodes = [self selectednodes];        if ([selectednodes count] > 1) {        NSLog(@"Multiple Rows Selected");    } else if ([selectednodes count] == 1) {        SimpleNodeData *data = [[selectednodes lastObject] representedobject];                NSLog(@"click tree:%@,",data.name);        [contentLabel setStringValue:data.name];    } }@end


自定义标签,用于定义表格的NSCell

MqjCenterTextFIEldCell.m

#import "MqjCenterTextFIEldCell.h"@implementation MqjCenterTextFIEldCell- (voID)drawWithFrame:(NSRect)cellFrame inVIEw:(NSVIEw *)controlVIEw{    NSRect rectCenter = cellFrame;    NSSize size = [[self Title] sizeWithAttributes:nil];    CGfloat offset = (rectCenter.size.height-size.height)/2;    rectCenter.origin.y = rectCenter.origin.y+offset;    rectCenter.size.height = cellFrame.size.height-offset;    [super drawWithFrame:rectCenter inVIEw:controlVIEw];}@end


2.设置代理和数据源



3.设置表格列元素的 IDentifIEr



4.设置NSOutlineVIEw对象和NSTextFIEld对象



5.做好以上步骤,运行结果如下:

整个项目代码下载网址:

http://download.csdn.net/detail/moqj_123/9146323

总结

以上是内存溢出为你收集整理的[Cocoa]_[初级]_[使用NSOutlineView创建多根显示]全部内容,希望文章能够帮你解决[Cocoa]_[初级]_[使用NSOutlineView创建多根显示]所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1051646.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存