MacOS开发 | NSOutlineView基于Cell Based

在我们进行开发的时候,想要展示一组层级结构的数据时,可以使用NSOutlineView来对层级结构的数据进行展示,在MacOS开发中,XCode给我们提供了两种样式的NSOutlineView:Outline ViewSource List,我这边就直接使用 Outline View样式的NSOutlineView

数据模型

因为在NSOutlineView需要显示出层级结构,因此Data model中要能够表示出这种数据的层级来:root node -> leaf node,所有我这边会创建两个类:RootDataLeafData

--- RootData.h
#import 
#import "LeafData.h"

NS_ASSUME_NONNULL_BEGIN
@interface RootData : NSObject
@property (nonatomic,copy) NSString *name;
@property (nonatomic,strong) NSArray *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 

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

实现数据的显示

  1. 首先拖拽一个 Outline View样式的 NSOutlineView 到View上,并修改 Content ModeColumns这两个属性,如图1
图1
  1. 将Outline View控件拖拽到ViewController.h中,取名为outlineView,并且在ViewController.h中遵循NSOutlineViewDelegate,NSOutlineViewDataSource协议,如图2
图2
  1. 添加如下的代码至ViewController.m中的viewDidLoad方法中
self.outlineView.delegate = self;
self.outlineView.dataSource = self;
  1. 为显示的数据进行赋值,如下代码,专门创建一个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];
}
  1. 实现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;
}
  1. 运行代码,界面展示如图3


    图3

你可能感兴趣的:(MacOS开发 | NSOutlineView基于Cell Based)