tableView的多级列表(深度优先访问)

1.先上一个gif 图片
tableView的多级列表(深度优先访问)_第1张图片
多级.gif
2.分析代码

首先我们应该知道树形的结构 ,这个应该属于典型的深度访问结构,接下来还是看看代码。

1.首先定义一个数据类型

.h 文件 
#import 

@interface MultiLevelModel : NSObject

@property (nonatomic,assign) NSInteger level;//层级
@property (nonatomic,copy)   NSString * name;//名字
@property (nonatomic,strong) NSMutableArray * subItems;//子数据
@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的一些创建


.h 文件 
#import 
#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 中的数据处理 我是先创建一个 测试的字典,通过这个字典转换成我们需要的数据类型

//
//  ViewController.m
//  多级tableView 点击
//
//  Created by apple on 16/6/7.
//  Copyright © 2016年 李重阳. All rights reserved.
//

#import "ViewController.h"
#import "MultiLevelCell.h"

@interface ViewController ()

@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];
    
    /*初始化数据源 **/
    _configDataArr = [[NSMutableArray alloc]init];
    _dataArr = [[NSMutableArray alloc]init];
    
    /*创建数据,模拟数据**/
    NSDictionary * dic = [self testDic];
    //把dic =》 原始含有model数据源
    [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];
    }
    
}


/* 创建测试的 数据源**/
- (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 初始数据源转换成 我们需要的数据类型**/
- (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];
            
        }
    }
    
    
    
}




/*
 *更新数据
 *深度优先 插入数据源
 **/
- (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


gitHub 的连接在这里

你可能感兴趣的:(tableView的多级列表(深度优先访问))