1.先上一個gif 圖片
2.分析代碼
首先我們應(yīng)該知道樹形的結(jié)構(gòu) ,這個應(yīng)該屬于典型的深度訪問結(jié)構(gòu),接下來還是看看代碼厕鹃。
1.首先定義一個數(shù)據(jù)類型
.h 文件
#import <Foundation/Foundation.h>
@interface MultiLevelModel : NSObject
@property (nonatomic,assign) NSInteger level;//層級
@property (nonatomic,copy) NSString * name;//名字
@property (nonatomic,strong) NSMutableArray * subItems;//子數(shù)據(jù)
@property (nonatomic,copy) NSString * dev_id;//部門的ID
@property (nonatomic,assign) BOOL isOpen;//是否開啟
@end
.m 文件
#import "MultiLevelModel.h"
@implementation MultiLevelModel
- (instancetype)init {
if (self = [super init]) {
_isOpen = NO;
_subItems = [[NSMutableArray alloc]init];
}
return self;
}
@end
就是cell的一些創(chuàng)建
.h 文件
#import <UIKit/UIKit.h>
#import "MultiLevelModel.h"
@interface MultiLevelCell : UITableViewCell
@property (nonatomic,strong) MultiLevelModel * levelModel;
@end
.m 文件
#import "MultiLevelCell.h"
@interface MultiLevelCell ()
@property (nonatomic,strong) UILabel * nameLabel;
@property (nonatomic,strong) UILabel * desLabel;
@end
@implementation MultiLevelCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
_nameLabel = [[UILabel alloc]init];
[self.contentView addSubview:_nameLabel];
_desLabel = [[UILabel alloc]init];
[self.contentView addSubview:_desLabel];
}
return self;
}
- (void)setLevelModel:(MultiLevelModel *)levelModel {
_levelModel = levelModel;
CGFloat nameX = 0;
CGFloat nameW = 100;
CGFloat nameH = 44;
CGFloat nameY = 0;
if (levelModel.level > 1) { //不是根目錄
nameX = 20;
}
self.nameLabel.frame = CGRectMake(nameX, nameY, nameW, nameH);
self.nameLabel.text = levelModel.name;
CGFloat desLabelX = CGRectGetMaxX(self.nameLabel.frame);
CGFloat desLabelY = 0;
CGFloat desLabelW = 50;
CGFloat desLabelH = 44;
self.desLabel.frame = CGRectMake(desLabelX, desLabelY, desLabelW, desLabelH);
self.desLabel.text = levelModel.subItems.count>0?@(levelModel.subItems.count).description:@"-";
if (levelModel.subItems.count) {
self.nameLabel.textColor = [UIColor redColor];
}else {
self.nameLabel.textColor = [UIColor blackColor];
}
}
@end
接下來就是在VC 中的數(shù)據(jù)處理 我是先創(chuàng)建一個 測試的字典疾牲,通過這個字典轉(zhuǎn)換成我們需要的數(shù)據(jù)類型
//
// ViewController.m
// 多級tableView 點(diǎn)擊
//
// Created by apple on 16/6/7.
// Copyright ? 2016年 李重陽. All rights reserved.
//
#import "ViewController.h"
#import "MultiLevelCell.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView * tableView;
@property (nonatomic,strong) NSMutableArray * dataArr;
@property (nonatomic,strong) NSMutableArray * configDataArr;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.view addSubview:self.tableView];
/*初始化數(shù)據(jù)源 **/
_configDataArr = [[NSMutableArray alloc]init];
_dataArr = [[NSMutableArray alloc]init];
/*創(chuàng)建數(shù)據(jù),模擬數(shù)據(jù)**/
NSDictionary * dic = [self testDic];
//把dic =》 原始含有model數(shù)據(jù)源
[self transitionToDataArrWithDic:dic level:1 dataArr:self.dataArr];
/* 刷新config**/
[self reloadTableViewDataAndConfig];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.configDataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MultiLevelCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LevelCell"];
if (cell == nil) {
cell = [[MultiLevelCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LevelCell"];
}
cell.levelModel = self.configDataArr[indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
MultiLevelModel *levelModel = _configDataArr[indexPath.row];
if (levelModel.subItems.count) {
levelModel.isOpen = !levelModel.isOpen;
[self reloadTableViewDataAndConfig];
}
}
/* 創(chuàng)建測試的 數(shù)據(jù)源**/
- (NSDictionary *)testDic {
NSMutableDictionary * sumDic = [NSMutableDictionary dictionary];
NSMutableArray * dataArr = [NSMutableArray array];
for (NSInteger i = 0; i < 3; i++) {
NSMutableDictionary * dic1 = [NSMutableDictionary dictionary];
dic1[@"name"] = [NSString stringWithFormat:@"name_%ld",i+1];
[dataArr addObject:dic1];
if (i == 0 || i == 1) {
NSMutableArray * dataArr2 = [NSMutableArray array];
for (NSInteger j = 0; j < 5; j++) {
NSMutableDictionary * dic2 = [NSMutableDictionary dictionary];
dic2[@"name"] = [NSString stringWithFormat:@"name_%ld",j+1];
[dataArr2 addObject:dic2];
if (j ==1) {
NSMutableArray * dataArr3 = [NSMutableArray array];
for (NSInteger k = 0; k < 5; k++) {
NSMutableDictionary * dic3 = [NSMutableDictionary dictionary];
dic3[@"name"] = [NSString stringWithFormat:@"name_%ld",k+1];
[dataArr3 addObject:dic3];
}
dic2[@"subItems"] = dataArr3;
}
}
dic1[@"subItems"] = dataArr2;
}
sumDic[@"subItems"] = dataArr;
}
return sumDic;
}
/* 把dic 初始數(shù)據(jù)源轉(zhuǎn)換成 我們需要的數(shù)據(jù)類型**/
- (void)transitionToDataArrWithDic:(NSDictionary *)dic level:(NSInteger)level dataArr:(NSMutableArray *)dataArr{
NSArray * arr = dic[@"subItems"];
/* 防止 arr 是字典類型 **/
if ([arr isKindOfClass:[NSDictionary class]]) {
arr = @[arr];
}
/*確保arr 是 NSArray 類型 **/
if ([arr isKindOfClass:[NSArray class]]) {
for (NSDictionary * subDic in arr) {
MultiLevelModel * model = [[MultiLevelModel alloc]init];
model.name = subDic[@"name"];
model.level = level;
[dataArr addObject:model];
[self transitionToDataArrWithDic:subDic level:level +1 dataArr:model.subItems];
}
}
}
/*
*更新數(shù)據(jù)
*深度優(yōu)先 插入數(shù)據(jù)源
**/
- (void)depthFirstUpdateTableViewDataWithItemArr:(NSArray *)ItemArr {
if (ItemArr.count > 0) {
for (MultiLevelModel * model in ItemArr) {
[self.configDataArr addObject:model];
if (model.subItems && model.isOpen) {
[self depthFirstUpdateTableViewDataWithItemArr:model.subItems];
}
}
}
}
/* 刷新config**/
- (void)reloadTableViewDataAndConfig {
[self.configDataArr removeAllObjects];
[self depthFirstUpdateTableViewDataWithItemArr:self.dataArr];
[self.tableView reloadData];
}
- (UITableView *)tableView {
if (_tableView == nil) {
_tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
}
return _tableView;
}
@end