宽容他人,放过自己。

点击cell展开cell

Posted on By anchoriteFili

cell改变高度模拟.zip

1. 创建tableView,添加对应的section及其对应的row的数量
#pragma mark 创建tableView
    self.tabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    
    self.tabelView.delegate = self;
    self.tabelView.dataSource = self;
    //    设置tableView背景色为透明色
    self.tabelView.backgroundColor = [UIColor clearColor];
    //    分割线样式
    //    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
#pragma mark model数组赋值
    [self.view addSubview:self.tabelView];
    
    //section个数
    for (int i = 0; i < 5; i ++) {
        Model *model = [[Model alloc] init];
            
        if (i == 0) {
            model.openBool = YES;
        }
        
        model.childModels = [NSMutableArray array];
        //row个数
        for (int j = 0; j <= i+5; j ++) {
            
            ChildModel *childModel = [[ChildModel alloc] init];
            childModel.labelText = [NSString stringWithFormat:@"第%d个选项",j];
            [model.childModels addObject:childModel];
            }
            
        [self.modelArr addObject:model];
    }
2. 设置行高度,当openBool显示的为YES的时候展开全页
#pragma mark 设置每行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    Model *model = [self.modelArr objectAtIndex:indexPath.row];
    //当是展开页面的时候,显示行高
    if (model.openBool) {
        return model.cellHeight;
    } else {
        return 40;
    }
}

#pragma mark 设置section的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

#pragma mark 设置每个section的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return 5;
}
3. 在cellForRow方法中,mode传值,刷新指定行
#pragma mark cellForRow方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    TestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[TestTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    Model *model = [self.modelArr objectAtIndex:indexPath.row];
    //添加行号
    model.index = indexPath.row;
    [cell updateDataWithModel:model];
    
    [cell setMyBlock:^(NSInteger index) {
        
        //刷新指定的行
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
        [self.tabelView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }];
    
    //    设置选择时的状态
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //    设置cell背景色为透明色
    cell.backgroundColor = [UIColor clearColor];
    
    return cell;
}
4. 点击cell后cell直接恢复常态
#pragma mark 添加点击cell的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   
    //点击以后直接恢复正常状态
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
cell中的部分
- (void)updateDataWithModel:(Model *)model {
    
    self.model = model;
    
    kButtonTitle(self.button, [NSString stringWithFormat:@"第%ld个button",(long)model.index], [UIColor whiteColor], 15);
    self.button.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 40);
    
    self.label.text = [NSString stringWithFormat:@"第%ld个label",(long)model.index];
    self.label.frame = CGRectMake(0, 40, [UIScreen mainScreen].bounds.size.width, 40);
    
#pragma mark 移除其所有的子视图
    [self.radioView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
#pragma mark 添加子视图
    float height = 0;
    for (int i  = 0; i < model.childModels.count; i ++) {
        ChildModel *childModel = [model.childModels objectAtIndex:i];
        
        UIButton *button = kButtonFrame(0, height, 60, 30);
        
        button.backgroundColor = [UIColor whiteColor];
        button.tag = 1000+i;
        UIColor *color = [UIColor redColor];
        kButtonTitle(button, @"空", color, 15);
        kButtonTarget(button, self, @selector(modelButtonClick:));
        [self.radioView addSubview:button];
        
        UILabel *label = kLabelFrame(GETWIDTH(button)+5, GETY(button), 300, 30);
        label.backgroundColor = [UIColor blueColor];
        label.text = childModel.labelText;
        [self.radioView addSubview:label];
        
        height = GETY(label)+GETHEIGHT(label)+5;
    }
    
    self.radioView.frame = CGRectMake(10, 40, WIDTH-20, height);
    //设置行高
    model.cellHeight = GETY(self.radioView)+GETHEIGHT(self.radioView)+5;
    
    //判断是展开状态还是关闭状态,用于隐藏控件
    if (self.model.openBool) {
        self.radioView.hidden = NO;
    } else {
        self.radioView.hidden = YES;
    }
    
}

#pragma mark 单选框的创建
- (void)modelButtonClick:(UIButton *)sender {
    
    NSLog(@"tag = %ld",(long)sender.tag);
    for (int i = 0; i < self.model.childModels.count; i++) {
        
        UIButton *button = [self.radioView viewWithTag:(1000+i)];
        [button setTitle:@"空" forState:UIControlStateNormal];
    }
    [sender setTitle:@"实" forState:UIControlStateNormal];
}

#pragma mark 点击展开和关闭页面
- (void)buttonClick:(UIButton *)sender {

    self.model.openBool = !self.model.openBool;
    
    if (self.model.openBool) {
        self.radioView.hidden = NO;
    } else {
        self.radioView.hidden = YES;
    }
    
    self.myBlock(self.model.index);
}