美文网首页
点击UITableView的cell展开收缩(二)

点击UITableView的cell展开收缩(二)

作者: 来敲代码 | 来源:发表于2018-08-16 09:29 被阅读49次

    点击组头 展开cell

    初始化和加载数据

    {
        self.table.tableFooterView = self.footerView;
        [self.table registerClass:[WETableViewCell class] forCellReuseIdentifier:@"WETableViewCell"];
        [self.view addSubview:self.table];
    }
    - (void)loadData
    {
        [[WENetworking shareInstance] ExaminationTypeListsuccess:^(id responseObject) {
         
            NSLog(@"%@",responseObject);
            NSArray *exam_typeArr= responseObject[@"data"][@"exam_type"];
            
            for (NSDictionary *exam_typedic in exam_typeArr) {
                
                
                headerModel *model = [[headerModel alloc] init];
                model.headerTitle = exam_typedic[@"exam_type_name"];
                model.examTypeCode = exam_typedic[@"exam_type_code"];
                ;            NSLog(@"默认%ld个cell",(model.cellsModel.count));
    
                model.isOpen = NO;
                
                
                [self.firstArr addObject:model];
    //            [self.firstArr addObject:exam_typedic[@"exam_type_name"]];
            }
            
            [self.table reloadData];
            
        } failure:^(NSError *error) {
            
        }];
    
    }
    

    自定义表尾 因为项目需求点击创建分类(创建组头)

    //  WEFooterView.h
    
    @interface WEFooterView : UIButton
    
    @property (nonatomic,strong) NSString *footerTitle;
    @property (nonatomic,copy) void (^footerBlock)(NSString *footerStr);
    @end
    //
    //  WEFooterView.m
    //  WisdomEducation
    
    #import "WEFooterView.h"
    @interface WEFooterView()
    @property(nonatomic, strong) UILabel *titleLab;
    
    @property(nonatomic, strong) UIImageView *addImageView;// 加号
    
    @end
    @implementation WEFooterView
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            [self addSubviews];
            [self addTarget:self action:@selector(footerClick) forControlEvents:UIControlEventTouchUpInside];
        }
        return self;
    }
    - (void)addSubviews
    {
        
        [self addSubview:self.titleLab];
        [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(10);
            make.centerY.offset(0);
        }];
        
        
        [self addSubview:self.addImageView];
        [self.addImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.offset(-36);
            make.centerY.offset(0);
            make.size.mas_equalTo(CGSizeMake(18, 18));
        }];
    }
    - (void)footerClick
    {
        if (_footerBlock != nil) {
            _footerBlock(_footerTitle);
        }
    }
    - (void)setFooterTitle:(NSString *)footerTitle
    {
        _footerTitle = footerTitle;
        self.titleLab.text = footerTitle;
    }
    -(UILabel *)titleLab
    {
        if (_titleLab == nil) {
            _titleLab = [[UILabel alloc] init];
            _titleLab.text = @"新建一级分类";
            _titleLab.font = [UIFont systemFontOfSize:16];
        }
        return _titleLab;
    }
    -(UIImageView *)addImageView
    {
        if (_addImageView ==nil) {
            _addImageView =[[UIImageView alloc] init];
            _addImageView.image = [UIImage imageNamed:@"加号"];
        }
        return _addImageView;
    }
    @end
    

    自定义cell

    //  WETableViewCell.h
    
    #import <UIKit/UIKit.h>
    
    @interface WETableViewCell : UITableViewCell
    @property (nonatomic,strong) NSString *firstCellTitle;
    
    /**
     选中状态
     */
    @property (nonatomic,assign) BOOL selectedState;
    @property (nonatomic,copy) void (^firstCellBlock)(BOOL selecte);
    @property (nonatomic,copy) void (^deleteCellBlock)(void);
    @property (nonatomic,copy) void (^editCellBlock)(void);
    
    @end
    
    //
    //  WETableViewCell.m
    
    #import "WETableViewCell.h"
    @interface WETableViewCell ()
    @property(nonatomic, strong) UILabel* titleLab;
    @property (nonatomic,strong)UIButton *deldteBtn;
    @property (nonatomic, strong)UIButton *editBtn;
    /**
     箭头
     */
    //@property(nonatomic, strong) UIImageView* indacateImageView;
    @end
    @implementation WETableViewCell
    
    - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
            
            [self addSubViews];
        }
        
        return self;
    }
    - (void)addSubViews
    {
        [self addSubview:self.titleLab];
        [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(25);
            make.centerY.offset(0);
    
        }];
        
        UIButton *deldteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [deldteBtn setImage:[UIImage imageNamed:@"删除"] forState:UIControlStateNormal];
        [deldteBtn addTarget:self action:@selector(deleteBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:deldteBtn];
        self.deldteBtn = deldteBtn;
        [self.deldteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            
                    make.right.offset(-40);
                    make.centerY.offset(0);
                    make.size.mas_equalTo(CGSizeMake(14, 14));
    
        }];
        
        UIButton *editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [editBtn setImage:[UIImage imageNamed:@"编辑"] forState:UIControlStateNormal];
        [editBtn addTarget:self action:@selector(editBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:editBtn];
        
        self.editBtn = editBtn;
        [self.editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.right.offset(-70);
            make.centerY.offset(0);
            make.size.mas_equalTo(CGSizeMake(14, 14));
    
            
        }];
    }
    
    - (void)setSelectedState:(BOOL)selectedState
    {
        _selectedState = selectedState;
        
        if (_firstCellBlock != nil) {
            _firstCellBlock(_selectedState);
            
        }
    }
    - (void)setFirstCellTitle:(NSString *)firstCellTitle
    {
        _firstCellTitle = firstCellTitle;
        self.titleLab.text = firstCellTitle;
        if ([firstCellTitle isEqualToString:@"二级创造"]) {
            [self.deldteBtn setImage:nil forState:UIControlStateNormal];
            [self.editBtn setImage:nil forState:UIControlStateNormal];
    
        }
    }
    
    -(UILabel *)titleLab
    {
        if (_titleLab == nil) {
            
            _titleLab = [[UILabel alloc] init];
            _titleLab.font = [UIFont systemFontOfSize:16];
    
        }
        return _titleLab;
    }
    - (void)deleteBtnAction
    {
        if (_deleteCellBlock != nil) {
            _deleteCellBlock();
        }
    }
    - (void)editBtnAction
    {
        if (_editCellBlock != nil) {
            _editCellBlock();
        }
    }
    - (void)awakeFromNib {
        [super awakeFromNib];
        // Initialization code
    }
    
    - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
        [super setSelected:selected animated:animated];
    
        // Configure the view for the selected state
    }
    
    @end
    
    

    自定义组头

    //
    //  WEHeaderView.h
    
    #import <UIKit/UIKit.h>
    #import "headerModel.h"
    
    @interface WEHeaderView : UIButton
    @property (nonatomic,strong) NSString *firstCellTitle;
    
    @property (nonatomic,copy) void (^firstCellBlock)(BOOL selecte);
    @property (nonatomic,strong) headerModel *sectionModel;
    
    @end
    
    //
    //  WEHeaderView.m
    #import "WEHeaderView.h"
    @interface WEHeaderView ()
    @property(nonatomic, strong) UILabel* titleLab;
    /**
     箭头
     */
    @property(nonatomic, strong) UIImageView* indacateImageView;
    
    @end
    @implementation WEHeaderView
    
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
                [self addSubViews];
            [self addTarget:self action:@selector(headerClick) forControlEvents:UIControlEventTouchUpInside];
        }
        return self;
    }
    
    - (void)addSubViews
    {
        
        UIImageView* leftLine = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"instructions_img_leftLine"]];
        [self addSubview:leftLine];
        [leftLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(10);
            make.centerY.offset(0);
        }];
        
        [self addSubview:self.titleLab];
        [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(18);
            make.centerY.offset(0);
            
        }];
        
        
        [self addSubview:self.indacateImageView];
        [self.indacateImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.offset(-36);
            make.centerY.offset(0);
            make.size.mas_equalTo(CGSizeMake(14, 9));
        }];
    }
    
    - (void)setFirstCellTitle:(NSString *)firstCellTitle
    {
        _firstCellTitle = firstCellTitle;
        self.titleLab.text = firstCellTitle;
    }
    - (void)setSectionModel:(headerModel *)sectionModel
    {
        _sectionModel = sectionModel;
        
    //    if (!_sectionModel.isOpen) {
    //
    //        self.indacateImageView.transform = CGAffineTransformIdentity;
    //
    //    }else{
    //
    //        self.indacateImageView.transform = CGAffineTransformMakeRotation(M_PI);
    //    }
        
                if (self.sectionModel.isOpen) {
                    self.indacateImageView.image = [UIImage imageNamed:@"instructions_img_up"];
                } else {
                    self.indacateImageView.image = [UIImage imageNamed:@"instructions_img_down"];
    
                }
    
    }
    -(UIImageView *)indacateImageView
    {
        if (_indacateImageView == nil) {
            _indacateImageView = [[UIImageView alloc] init];
            _indacateImageView.image = [UIImage imageNamed:@"instructions_img_down"];
            
        }
        return _indacateImageView;
    }
    -(UILabel *)titleLab
    {
        if (_titleLab == nil) {
            
            _titleLab = [[UILabel alloc] init];
            _titleLab.font = [UIFont systemFontOfSize:16];
            
        }
        return _titleLab;
    }
    #pragma mark _ header 点击
    - (void)headerClick {
        
        self.sectionModel.isOpen = !self.sectionModel.isOpen;
    
        
    //    [UIView animateWithDuration:0.25 animations:^{
    //
    //        if (!self.sectionModel.isOpen) {
    //
    //            self.indacateImageView.transform = CGAffineTransformIdentity;
    //
    //        }else{
    //
    //            self.indacateImageView.transform = CGAffineTransformMakeRotation(M_PI);
    //        }
    //
    //    }];
        
            if (self.sectionModel.isOpen) {
                self.indacateImageView.image = [UIImage imageNamed:@"instructions_img_up"];
            } else {
                self.indacateImageView.image = [UIImage imageNamed:@"instructions_img_down"];
    
            }
        
        if (_firstCellBlock != nil) {
            _firstCellBlock(self.sectionModel.isOpen);
            
        }
    }
    
    @end
    

    cellModel 组头的Model 重点-->组头model用属性数组包含cellModel 用来属性没组多少cell

    cell 和组头 上的点击回调

    #pragma mark  多少组
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return self.firstArr.count;
    }
    #pragma mark  每组多少个cell
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    //    return self.senondArr.count;
        NSLog(@"每组有%ld个cell",(self.firstArr[section].cellsModel.count));
        return self.firstArr[section].isOpen ? (self.firstArr[section].cellsModel.count) : 0;
        
    }
    #pragma mark  cell多高
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return 44.f;
    }
    #pragma mark  组头多高
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
        return 44.f;
    }
    - (UIView* )tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        __weak typeof(self) weakSelf = self;
        
        WEHeaderView* sectionView = [WEHeaderView buttonWithType:UIButtonTypeCustom];
    //    sectionView.selectedState = _headerSelectedArr[section];
        
        sectionView.frame = CGRectMake(0, 0, KScreenWidth, 44);
        
        headerModel *headerModel = [self.firstArr safeObjectAtIndex:section];
        sectionView.sectionModel = headerModel;
    
        sectionView.firstCellTitle = headerModel.headerTitle;
        
        [sectionView setFirstCellBlock:^(BOOL selected) {
            
            [weakSelf setupSelectSecionView:section selectedState:selected];
            
        }];
        
        return  sectionView;
    }
    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
        return 0.01f;
    }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        
        WETableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WETableViewCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
        NSArray *cellarr = self.firstArr[indexPath.section].cellsModel;
        cellModel *cellmodel =cellarr[indexPath.row];
        
        cell.firstCellTitle = cellmodel.title;
        
        [cell setDeleteCellBlock:^{
            
            [self deleteCell:cellmodel CellNSIndexPath:indexPath];
        }];
        return cell;
    }
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        WETableViewCell *cell = (WETableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
        
        cell.selectedState = !cell.selectedState;
    
        if ([cell.firstCellTitle isEqualToString:@"二级创造"]) {
            
            [self createAlert:SecondCreateType examTypename:indexPath.section];
        }
        
        
        [cell setFirstCellBlock:^(BOOL selecte) {
            
               NSLog(@"点击了%ld----->%ld",indexPath.row,selecte);
        }];
        
        [cell setEditCellBlock:^{
            
            NSLog(@"编辑----->%ld",indexPath.row);
    
        }];
    }
    #pragma mark - 选中哪一个组头 组头回调点击事件
    - (void)setupSelectSecionView:(NSInteger)section selectedState:(BOOL)selected  {
        
        
        NSLog(@"第%ld组------>%ld",(long)section,selected);
        
        headerModel *ExamTypeModel = self.firstArr[section];
        NSString *ExamTypeCode = ExamTypeModel.examTypeCode;
        
        if (ExamTypeCode.length > 0) {
            
            [ExamTypeModel.cellsModel removeAllObjects];
            cellModel *model = [[cellModel alloc] init];
            
            model.examSubtypeCode = nil;
            model.title = @"二级创造";
            [ExamTypeModel.cellsModel addObject:model];
    
            [[WENetworking shareInstance] createSubTypeListExamTypeCode:ExamTypeCode success:^(id responseObject) {
                
                if ([responseObject[@"msg"] isEqualToString:@"ok"]) {
                    
                    NSArray *subtypes = responseObject[@"data"][@"exam_subtypes"];
                    
                    for (NSDictionary *subtypedic in subtypes) {
                        
                        cellModel *model = [[cellModel alloc] init];
                        
                        model.examSubtypeCode = subtypedic[@"exam_subtype_code"];
                        model.title = subtypedic[@"exam_subtype_name"];
                        [ExamTypeModel.cellsModel insertObject:model atIndex:0];
                    }
                }
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    [self.table reloadSections:[[NSIndexSet alloc] initWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic];
    
                });
                
            } failure:^(NSError *error) {
                
                NSLog(@"查询二级列表错误:%@",error);
            }];
        }
    
        
    }
    #pragma mark 删除考试子类
    - (void)deleteCell:(cellModel *)cellmode CellNSIndexPath:(NSIndexPath *)indexPath
    {
        
        NSString *SubtypeCode = cellmode.examSubtypeCode;
        
        [[WENetworking shareInstance] deleteSubTestName:SubtypeCode success:^(id responseObject) {
            
            if ([responseObject[@"msg"] isEqualToString:@"ok"]) {
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    [MBProgressHUD showTipMessageInWindow:@"删除成功!" timer:2.0];
    
                    [self setupSelectSecionView:indexPath.section selectedState:YES];
    
                });
    
            }
            
        } failure:^(NSError *error) {
            
        }];
        
    }
    #pragma mark 懒加载
    -(UITableView *)table
    {
        if (_table == nil) {
            
            _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight-64) style:UITableViewStylePlain];
            _table.separatorStyle = UITableViewCellSeparatorStyleNone;
            _table.delegate = self;
            _table.dataSource = self;
            _table.estimatedRowHeight = 0;
        }
        return _table;
    }
    -(WEFooterView *)footerView
    {
        
        __weak typeof(self) weakSelf = self;
    
        if (_footerView == nil) {
            
            _footerView = [WEFooterView buttonWithType:UIButtonTypeCustom];
            _footerView.frame = CGRectMake(0, 0, KScreenWidth, 44);
            _footerView.footerTitle = @"新建一级分类";
            
            [_footerView setFooterBlock:^(NSString *footerStr) {
                
                NSLog(@"点击了表尾  创建一级分类! 弹框");
          
                [weakSelf createAlert:FirstCreateType examTypename:0];
    
            }];
        }
        return _footerView;
    }
    - (void)createAlert:(AlertType)type  examTypename:(NSInteger )type_name
    {
        __weak typeof(self) weakSelf = self;
    
        dispatch_async(dispatch_get_main_queue(), ^{
            
            //                [WEAlertView alerteShowCreateType:FirstCreateType TextFileBlock:nil];
            
            if (!weakSelf.CreateAlertView) {
                
                weakSelf.CreateAlertView = [[FirstCreateView alloc] initWithFrame:CGRectMake(KScreenWidth/2-150, KScreenHeight/2 - 230, 300, 300)];
                weakSelf.CreateAlertView.alertType = type;
                [weakSelf.view addSubview:weakSelf.CreateAlertView];
                
                [weakSelf.CreateAlertView setDismissBlock:^{
                    weakSelf.CreateAlertView = nil;
                }];
                
                [weakSelf.CreateAlertView setTextBlock:^(NSString *CreateName) {
                    
                    NSLog(@"%@",CreateName);
                    if (type == FirstCreateType) {
                        
                        [weakSelf CreateFirst:CreateName];
                        
                    }else{
                        
                        [weakSelf CreateSecond:CreateName Type_name:type_name];
                    }
                    
                }];
                
            }
            
            
        });
    
    }
    -(NSMutableArray *)firstArr
    {
        if (!_firstArr) {
            
            _firstArr = [[NSMutableArray alloc] init];
        }
        return _firstArr;
    }
    - (NSMutableArray *)senondArr
    {
        if (!_senondArr) {
            _senondArr = [[NSMutableArray alloc] init];
        }
        return _senondArr;
    }
    
    #pragma mark 创建一级分类
    - (void)CreateFirst:(NSString *)firstrName
    {
        NSDictionary *dic = @{
                              @"exam_type_name":firstrName,
                              @"short_name":@"TestExamination"
                              };
    
        [[WENetworking shareInstance] createExaminationType:dic success:^(id responseObject) {
            
            NSLog(@"%@",responseObject);
    
            NSDictionary *exam_typedic = responseObject[@"data"];
            
            headerModel *model = [[headerModel alloc] init];
            model.headerTitle = exam_typedic[@"exam_type_name"];
            model.examTypeCode = exam_typedic[@"exam_type_code"];
    
            model.isOpen = NO;
            [self.firstArr addObject:model];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
           
                [self.table reloadData];
            });
            
        } failure:^(NSError *error) {
            
        }];
        
    }
    
    
    /**
     @param secondName 创造二级类型名称
     @param type_name 所属一级分类名称
     */
    - (void)CreateSecond:(NSString *)secondName Type_name:(NSInteger )type_nameIndex
    {
        
        
        headerModel *model = self.firstArr[type_nameIndex];
    
        NSString *type_nameCode = model.examTypeCode;
        
        NSDictionary *dic = @{
                              @"exam_type_code":type_nameCode,
                              @"exam_subtype_name":secondName
                              };
    
        [[WENetworking shareInstance] createSubExamination:dic success:^(id responseObject) {
    
            if ([responseObject[@"msg"] isEqualToString:@"ok"]) {
                
                cellModel *secondModel = [[cellModel alloc]init];
                secondModel.title = responseObject[@"data"][@"exam_subtype_name"];
                secondModel.examSubtypeCode = responseObject[@"data"][@"exam_subtype_code"];
    
                [model.cellsModel insertObject:secondModel atIndex:0];
                
                dispatch_async(dispatch_get_main_queue(), ^{
                
                    NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:type_nameIndex];
                    
                    [self.table reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
                });
                
            }
            
            
        } failure:^(NSError *error) {
    
        }];
        
    }
    

    相关文章

      网友评论

          本文标题:点击UITableView的cell展开收缩(二)

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