#iOS分级展开

作者: Terry_S | 来源:发表于2016-06-23 15:41 被阅读902次

    按数据进行展开,最多二级(三级目录)。

    自适应展开的情况,可能是一层目录(不展开)二级目录(展开一级)三级目录展开两级

    写作原因:
    1.最近做的一个需求,感觉不错。
    2.git上没发现,大多数是一级展开,像qq那样,没发现二级展开的代码

    可以得到的东西:自适应展开,二级目录的一级展开,三级目录的二级展开

    思路:最难的无非是三级的情况,此时我们第一级用tableView的headerView自定义作为第一级,点击展开的tableView作为第二级数据显示,点击tableview的每一行,我们在cell里面用tableview作为他的第三级进行显示。 其它的如果是二级的话,tableview的headerView作为第一级,点击展开的tableview作为第二级。 如果是一级的情况,只需要把tableview的header作为第二级。

    1.首先建立tableView,不在赘述,但tableview得类型要用UITableViewStyleGrouped

    2.第二处理数据源,将其处理成模型,放在数组中,不在赘述

    3.tableView的代理

    #promark -- UITableViewDataSource
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        
        return _endArray.count;
        
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        
        ParentModel *pmodel = _endArray[section];
        if (pmodel.unfold) {
            return pmodel.children.count;
        }
        return 0;
        
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        
        ParentModel *pmodel = _endArray[indexPath.section];
        ParentModel *cmodel = pmodel.children[indexPath.row];
        if ([cmodel.type isEqualToString:@"unit"]) {
            TEFoldTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:foldCellIdentifier];
            if (!cell) {
                cell = [[[NSBundle mainBundle]loadNibNamed:foldCellIdentifier owner:self options:nil]lastObject];
            }
            if (indexPath.row == 0 && cmodel.unfold) {
                cell.lineView.backgroundColor = [UIColor colorFromHexRGB:@"0xdddddd"];
            }
            [cell cellConfigureCellFromModel:cmodel];
            cell.delegate = self;
            return cell;
        }
        CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:customTableViewCellIdentifier];
        if (!cell) {
            cell = [[[NSBundle mainBundle]loadNibNamed:customTableViewCellIdentifier owner:self options:nil]lastObject];
        }
        
        [cell cellConfigureContentFromModel:cmodel];
        return cell;
        
    }
    
    
    
    #pragma mark -- UITableViewDelegate
    
    -(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section
    {
        
        return 0.01f;
        
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        ParentModel *testModel = _endArray[indexPath.section];
        ParentModel *childModel = testModel.children[indexPath.row];
        if (childModel.unfold && [childModel.type isEqualToString:@"unit"]) {
            return childModel.children.count * 62 + 45;
        }
        if ([childModel.type isEqualToString:@"lesson"]) {
            return 62;
        }
        return 45;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        
        ParentModel *testModel = _endArray[indexPath.section];
        ParentModel *childModel = testModel.children[indexPath.row];
        childModel.unfold = !childModel.unfold;
        
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    //    [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
        [tableView deselectRowAtIndexPath:indexPath animated:true];
    }
    
    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
        
        ParentModel *pmodel = _endArray[section];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        button.tag = section + 1;
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
        
        
        if ([pmodel.type isEqualToString:@"lesson"]) {
            button.frame = CGRectMake(0, 0, self.view.frame.size.width, 62);
            
            UIImageView *leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 17, 17)];
            UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(leftImageView.frame) + 10, 10, CGRectGetWidth(self.view.frame) - CGRectGetMaxX(leftImageView.frame) - 10, 20)];
            titleLabel.font = [UIFont systemFontOfSize:14];
            titleLabel.textAlignment = NSTextAlignmentLeft;
            titleLabel.textColor = [UIColor colorFromHexRGB:@"0x666666"];
            titleLabel.text = pmodel.name;
            
            [button addSubview:titleLabel];
            
            UILabel *subTitleLable = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(titleLabel.frame) + 5, CGRectGetMaxY(titleLabel.frame) + 5, self.view.frame.size.width/4, 15)];
            subTitleLable.font = [UIFont systemFontOfSize:11];
            subTitleLable.textColor = [UIColor colorFromHexRGB:@"0x999999"];
            subTitleLable.text = pmodel.type;
            [button addSubview:subTitleLable];
            
            UILabel *liveStatusLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(subTitleLable.frame) + 8, CGRectGetMinY(subTitleLable.frame), 35, 15)];
            liveStatusLabel.layer.cornerRadius = 7;
            liveStatusLabel.layer.masksToBounds = YES;
            liveStatusLabel.backgroundColor = [UIColor colorFromHexRGB:@"0xe93131"];
            liveStatusLabel.textColor = [UIColor colorFromHexRGB:@"0xfffefe"];
            liveStatusLabel.text = @"直播中";//通过判断时间来判断是否直播中,或已结束
            liveStatusLabel.font = [UIFont systemFontOfSize:11];
            [button addSubview:liveStatusLabel];
            liveStatusLabel.hidden = YES;
            
            switch ([pmodel.lesson_type integerValue]) {
                case 1:
                    leftImageView.image = [UIImage imageNamed:@"play-round"];
                    subTitleLable.text = [NSString stringWithFormat:@"%@分钟",pmodel.media_length];
                    break;
                case 2:
                case 4:
    #warning 这里为直播,显示直播时间,还要判断是否在直播中,无数据,有数据需要完善。
                    
                    leftImageView.image = [UIImage imageNamed:@"zb"];
                    NSLog(@"等待处理时间戳转换");
                    break;
                case 3:
                case 8:
                    leftImageView.image = [UIImage imageNamed:@"cl"];
                    subTitleLable.text = @"试卷";
                    break;
                case 5:
                case 6:
                case 7:
                    leftImageView.image = [UIImage imageNamed:@"tkd"];
                    subTitleLable.text = @"材料";
                    break;
                    
                default:
                    break;
            }
            [button addSubview:leftImageView];
            
        } else {
            button.frame = CGRectMake(0, 0, self.view.frame.size.width, 45);
            
            UILabel *tlabel = [[UILabel alloc]initWithFrame:CGRectMake(10, (44-20)/2, 200, 20)];
            [tlabel setBackgroundColor:[UIColor clearColor]];
            [tlabel setFont:[UIFont systemFontOfSize:16]];
            tlabel.textColor = [UIColor colorFromHexRGB:@"0x333333"];
            [tlabel setText:[pmodel.name stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
            [button addSubview:tlabel];
            
            UIImageView *rightImageVIew = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 21,button.frame.size.height / 2 -5 , 11, 11)];
            if ([pmodel.type isEqualToString:@"unit"]) {
                rightImageVIew.frame = CGRectMake(self.view.frame.size.width - 21,button.frame.size.height / 2 -5 , 11, 7);
                if (pmodel.unfold) {
                    rightImageVIew.image = [UIImage imageNamed:@"slgx"];
                    
                } else {
                    rightImageVIew.image = [UIImage imageNamed:@"slgx1"];
                    
                }
            }else {
                if (pmodel.unfold) {
                    rightImageVIew.image = [UIImage imageNamed:@"xcjq"];
                } else {
                    rightImageVIew.image = [UIImage imageNamed:@"xcjq1"];
                    
                }
            }
            [button addSubview:rightImageVIew];
        }
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(button.frame) - 3, self.view.frame.size.width, 3)];
        if (pmodel.unfold) {
            lineView.frame = CGRectMake(0, CGRectGetHeight(button.frame) - 1, self.view.frame.size.width, 1);
        } else {
            lineView.frame = CGRectMake(0, CGRectGetHeight(button.frame) - 3, self.view.frame.size.width, 3);
        }
        lineView.backgroundColor = [UIColor colorFromHexRGB:@"0xdddddd"];
        
        [button addSubview:lineView];
        
        return  button;
    }
    
    
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        ParentModel *model = _endArray[section];
        if ([model.type isEqualToString:@"lesson"]) {
            return 62;
        }
        return 44;
        
    }
    
    
    #headerView的点击方法
    - (void)buttonPress:(UIButton *)sender//headButton点击
    {
        ParentModel *model = _endArray[sender.tag - 1];
        //判断状态值
        if (![model.type isEqualToString:@"lesson"]) {
            model.unfold = !model.unfold;
            
        }
        [_listTableView reloadSections:[NSIndexSet indexSetWithIndex:sender.tag-1] withRowAnimation:UITableViewRowAnimationAutomatic];
        
    }
    
    

    model的属性

    /**自己独一无二的id 不要与包id混乱*/
    @property (nonatomic, copy) NSString *courseId;
    /**标题*/
    @property (nonatomic, copy) NSString *name;
    @property (nonatomic, copy) NSString *number;
    /**类型 章节*/
    @property (nonatomic, copy) NSString *type;
    /**课件类型*/
    @property (nonatomic, copy) NSString *lesson_type;
    @property (nonatomic, copy) NSMutableArray *children;
    /**是否打开*/
    @property (nonatomic, assign) BOOL unfold;
    
    /**如果是视频视频长度,否则返回直播开始时间*/
    @property (nonatomic, copy) NSString *media_length;
    /**webView链接*/
    @property (nonatomic, copy) NSString *test_paper_link;
    
    

    TEFoldTableViewCell的实现

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return _childArray.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *ID = @"CustomTableViewCell";
        CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (!cell) {
            cell = [[[NSBundle mainBundle]loadNibNamed:ID owner:self options:nil]lastObject];;
        }
        ParentModel * model = _childArray[indexPath.row];
        [cell cellConfigureContentFromModel:model];
        cell.titleNameLabel.text = [model.name stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        return cell;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        return 62;
        
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        ParentModel *model = _childArray[indexPath.row];
        if ([self.delegate respondsToSelector:@selector(TableViewCellDidSelectWithLessonModel:)]) {
            [self.delegate TableViewCellDidSelectWithLessonModel:model];
        }
        [tableView deselectRowAtIndexPath:indexPath animated:true];
        
    }
    
    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
            [cell setSeparatorInset:UIEdgeInsetsZero];
        }
        if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
            [cell setLayoutMargins:UIEdgeInsetsZero];
        }
    }
    
    //更换图片
    -(void)setArrowImageIfUnfold:(BOOL)unfold {
        
        if (unfold) {
            [self.imageButton setImage:[UIImage imageNamed:@"slgx"] forState:UIControlStateNormal];
            
        } else {
            [self.imageButton setImage:[UIImage imageNamed:@"slgx1"] forState:UIControlStateNormal];
    
        }
    
    }
    
    - (void)cellConfigureCellFromModel:(ParentModel *)model {
        
        self.nameLabel.text = model.name;
        self.childArray = model.children;
        [self setArrowImageIfUnfold:model.unfold];
    
    }
    
    

    最底层的CustomTableVIewCell的xib不在讲述

    最新代码已经上传到git

    如果你觉得有用就加个star吧

    演示效果 imageimage

    相关文章

      网友评论

      • 雪_晟:如果是动态的数据 怎么返回高度
        Terry_S:@miss李manman 动态高度,难道每个cell的高度也会不同吗?,但是也会相对固定吧,不可能每个cell的高度不一样吧?
        雪_晟:@Terry_S 你这个高度给死了 如果是动态高度 怎么在外层的UITableview返回高度里写 还望指点下
        Terry_S:@miss李manman 首先每个最低级的cell的高度肯定是一样的,然后最外层的tableview的frame你应该也知道,需要计算的就是二级展开里面的那个Tableview的高度,return childModel.children.count * 62 + 45;这里就是计算高度的啊
      • Yaanco:666
        Terry_S:😊
      • 船长_:不错哦
        Terry_S:@船长_ 😂

      本文标题:#iOS分级展开

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