iOS 三级列表

先看效果

效果图.gif

先上代码,在讲思路!!

创建tableView
-(UITableView *)table{
    if (!_table) {
        _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        _table.delegate = self;
        _table.dataSource = self;
        _table.tableFooterView = [UIView new];
        [myCell registerTableViewWith:_table];
    }
    return _table;
}
创建总的数据模型,后面只操作这一个模型数组
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = @[].mutableCopy;
    }
    return _dataArr;
}
模型结构
@interface ListModel : NSObject

@property (nonatomic ,copy) NSString *name;
///是否展开收起
@property (nonatomic ,assign) BOOL isOpen;

///子节点
@property (nonatomic ,strong) NSMutableArray *array;

@property (nonatomic ,assign) int leave;

@end
模拟数据,通过leave来区别是第几级数据:0是一级数据,1是二级数据,2是三级数据
-(void)initData{
    for (int i = 0; i < 10; i++) {
        ListModel *model = [[ListModel alloc] init];
        model.name = [NSString stringWithFormat:@"%d", I];
        model.array = @[].mutableCopy;
        model.leave = 0;
        
        for (int j = 0; j < 3; j++) {
            ListModel *sonModel = [[ListModel alloc] init];
            sonModel.leave = 1;
            sonModel.array = @[].mutableCopy;
            sonModel.name = [NSString stringWithFormat:@"      j-%d",j];
            [model.array addObject:sonModel];
            
            for (int p = 0; p < 4; p++) {
                ListModel *nextModel = [[ListModel alloc] init];
                nextModel.leave = 2;
                nextModel.name = [NSString stringWithFormat:@"   p-%d",p];
                [sonModel.array addObject:nextModel];
            }
        }
        [self.dataArr addObject:model];
    }
    [self.table reloadData];
}
自定义cell
@interface myCell : UITableViewCell
+(NSString *)identifier;
+(void)registerTableViewWith:(UITableView *)table;
@property (nonatomic ,strong) UILabel *nameLab;
@property (nonatomic ,strong) UILabel *fLab;
@end

@implementation myCell


-(UILabel *)nameLab{
    if (!_nameLab) {
        _nameLab = [[UILabel alloc] initWithFrame:CGRectMake(100, 14, 100, 25)];
    }
    return _nameLab;
}

-(UILabel *)fLab{
    if (!_fLab) {
        _fLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 14, 40, 25)];
        _fLab.text = @"展开";
    }
    return _fLab;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.nameLab];
        [self.contentView addSubview:self.fLab];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

+(NSString *)identifier{
    return NSStringFromClass([self class]);
}

+(void)registerTableViewWith:(UITableView *)table{
    [table registerClass:[self class] forCellReuseIdentifier:[self identifier]];
}

@end
核心代码
#pragma mark tableView的代理
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    myCell *cell = [tableView dequeueReusableCellWithIdentifier:[myCell identifier]];
    
    ListModel *model = self.dataArr[indexPath.row];
    cell.nameLab.text = model.name;
    
    if (model.leave == 1) {
        cell.backgroundColor = UIColor.redColor;
        cell.fLab.frame = CGRectMake(25, 14, 40, 25);
    }
    else if (model.leave == 2){
        cell.backgroundColor = UIColor.brownColor;
        cell.fLab.frame = CGRectMake(35, 14, 40, 25);
    }
    else{
        cell.backgroundColor = UIColor.whiteColor;
        cell.fLab.frame = CGRectMake(10, 14, 40, 25);
    }
    return cell;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
tableViewCell 点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    myCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    ListModel *model = self.dataArr[indexPath.row];
    model.isOpen = !model.isOpen;
    self.rowCount = indexPath.row;
    if (model.isOpen) {
        [self insertDataArr:model.array];
        cell.fLab.text = @"收起";
    }else{
        //删除添加的model
        [self deleteRowWith:model.array deleteRow:indexPath.row];
        cell.fLab.text = @"展开";
    }
}
///插入cell
-(void)insertDataArr:(NSMutableArray *)array{
    NSMutableArray *arr = [NSMutableArray array];
    [self insertModelArr:array resultArray:arr];
    [self.table beginUpdates];
    [self.table insertRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.table endUpdates];
}

-(void)insertModelArr:(NSMutableArray *)array resultArray:(NSMutableArray *)resultArr{
    for (int i = 0; i < array.count; i++) {
        ListModel *model = array[I];
        self.rowCount++;
        [self.dataArr insertObject:model atIndex:self.rowCount];
        NSIndexPath *index = [NSIndexPath indexPathForRow:self.rowCount inSection:0];
        [resultArr addObject:index];
        if (model.isOpen) {
            [self insertModelArr:model.array resultArray:resultArr];
        }
    }
}

///删除cell
-(void)deleteRowWith:(NSMutableArray *)array deleteRow:(NSInteger)row{
    NSMutableArray *arr = [NSMutableArray array];
    [self deleteRow:array deleteRow:row resultArray:arr];
    [self.table beginUpdates];
    [self.table deleteRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.table endUpdates];
}

-(void) deleteRow:(NSMutableArray *)array deleteRow:(NSInteger)row resultArray:(NSMutableArray *)resultArr{
    for (int i = 0; i < array.count; i++) {
        ListModel *model = array[I];
        [self.dataArr removeObject:model];
        ++row;
        NSIndexPath *index = [NSIndexPath indexPathForRow:row inSection:0];
        [resultArr addObject:index];
        if (model.isOpen == YES) {
            [self deleteRow:model.array deleteRow:row resultArray:resultArr];
            NSIndexPath *index = resultArr.lastObject;
            row = index.row;
        }
    }
}

思路:

点击哪个cell 就把对用的模型 放到 self.dataArr 这个数组中,然后刷新列表达到展开效果,收起的话也是通过删除对应的模型再刷新列表。

你可能感兴趣的:(iOS 三级列表)