美文网首页
iOS 三级列表

iOS 三级列表

作者: 至尊宝_4233 | 来源:发表于2022-01-06 17:54 被阅读0次

先看效果

效果图.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 三级列表

    先看效果 先上代码,在讲思路!! 创建tableView 创建总的数据模型,后面只操作这一个模型数组 模型结构 模...

  • iOS之三级列表

    两级折叠列表的链接项目中有遇到做三级列表的功能出来,找了找demo,然后自己做了下,完善了些相关的UI界面,发出来...

  • 开发资料

    开源ReactNative项目列表 学习资料-书籍 iOS文章列表 iOS开源列表 Awesome iOS A c...

  • markdown测试

    段落 三级标题 四级标题 五级标题 列表 无序列表 列表1 列表2 列表3 列表1 列表2 列表3 有序列表 列表...

  • MarkDown使用

    MarkDowm使用 一 标题 一级标题 二级标题 三级标题 ... 二 列表 有序列表 列表 列表 列表 无序列...

  • Markdown语法学习

    标题 一级标题 二级标题 三级标题 四级标题 五级标题 六级标题 列表 列表 列表 列表 列表 列表 列表 列表 ...

  • TableView结合CollectionView展示三级列表

    先来看一张效果图 需求分析 做一个商品分类展示,有三级列表,要求第一级列表能展开,显示二三级列表,同时就还能收起。...

  • 2019-10-16

    测试简书编辑器 markdown 一级标题 二级标题 三级标题 三级标题 五级标题 列表第一项 列表第二项 列表第...

  • iOS_三级分类列表实现

    1.效果图如下: 直接上代码了哈,其实是3个tableView封装在View里面,看代码就明白了. view.h ...

  • markdown 常用语法

    一级标题 三级标题 列表1 列表2 列表3 列表4[简书链接](http://www.jianshu.com) 眉...

网友评论

      本文标题:iOS 三级列表

      本文链接:https://www.haomeiwen.com/subject/qodrcrtx.html