在我们进行开发的时候,想要展示一组层级结构的数据时,可以使用NSOutlineView来对层级结构的数据进行展示,在MacOS开发中,XCode给我们提供了两种样式的NSOutlineView:Outline View 和 Source List,我这边就直接使用 Outline View样式的NSOutlineView
数据模型
因为在NSOutlineView需要显示出层级结构,因此Data model中要能够表示出这种数据的层级来:root node -> leaf node,所有我这边会创建两个类:RootData
和 LeafData
--- RootData.h
#import <Foundation/Foundation.h>
#import "LeafData.h"
NS_ASSUME_NONNULL_BEGIN
@interface RootData : NSObject
@property (nonatomic,copy) NSString *name;
@property (nonatomic,strong) NSArray<LeafData *> *leafDataArray;
- (instancetype)initWithName:(NSString *)name;
@end
NS_ASSUME_NONNULL_END
--- RootData.m
#import "RootData.h"
@implementation RootData
- (instancetype)initWithName:(NSString *)name {
self = [super init];
if (self) {
_name = name;
}
return self;
}
@end
--- LeafData.h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface LeafData : NSObject
@property (nonatomic,copy) NSString *name;
- (instancetype)initWithName:(NSString *)name;
@end
NS_ASSUME_NONNULL_END
--- LeafData.m
#import "LeafData.h"
@implementation LeafData
- (instancetype)initWithName:(NSString *)name {
self = [super init];
if (self) {
_name = name;
}
return self;
}
@end
实现数据的显示
- 首先拖拽一个 Outline View样式的 NSOutlineView 到View上,并修改
Content Mode
和Columns
这两个属性,如图1
- 将Outline View控件拖拽到
ViewController.h
中,取名为outlineView,并且在ViewController.h
中遵循NSOutlineViewDelegate,NSOutlineViewDataSource协议,如图2
- 添加如下的代码至
ViewController.m
中的viewDidLoad
方法中
self.outlineView.delegate = self;
self.outlineView.dataSource = self;
- 为显示的数据进行赋值,如下代码,专门创建一个
addDatas
方法来进行赋值,并创建一个dataArr
属性来存储RootData对象
- (void)viewDidLoad {
[super viewDidLoad];
self.outlineView.delegate = self;
self.outlineView.dataSource = self;
[self addDatas];
}
- (void)addDatas {
RootData *root = [[RootData alloc] initWithName:@"国家"];
LeafData *leaf1 = [[LeafData alloc] initWithName:@"小明"];
LeafData *leaf2 = [[LeafData alloc] initWithName:@"小强"];
root.leafDataArray = @[leaf1,leaf2];
self.dataArr = [NSMutableArray arrayWithObject:root];
[self.outlineView reloadData];
}
- 实现NSOutlineViewDataSource协议中的必须实现的方法
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(nullable id)item {
if (item == nil) {
return self.dataArr.count;
}
if ([item isKindOfClass:[RootData class]]) {
RootData *root = (RootData *)item;
return root.leafDataArray.count;
}
return 0;
}
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(nullable id)item {
if (item == nil) {
return self.dataArr[index];
}
if ([item isKindOfClass:[RootData class]]) {
RootData *root = (RootData *)item;
LeafData *leaf = root.leafDataArray[index];
return leaf;
}
return nil;
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
if ([item isKindOfClass:[RootData class]]) {
return YES;
} else {
return NO;
}
}
- (nullable id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(nullable NSTableColumn *)tableColumn byItem:(nullable id)item {
if ([item isKindOfClass:[RootData class]]) {
RootData *root = (RootData *)item;
return root.name;
}
LeafData *leaf = (LeafData *)item;
return leaf.name;
}
-
运行代码,界面展示如图3
图3
网友评论