关于IOS UITableView的动态高度问题

IOS的UITableView生命周期是先加载heightForRowAtIndexPath,再加载cellForRowAtIndexPath。

需求要点击CELL在CELL中要显示隐藏的VIEW,所以CELL的高度需要动态计算。

所以不能在cellForRowAtIndexPath里面算高度,我用VO对象存储了CELL的高度来初始化高度,点击表格的事件中动态计算高度并且保存在VO对象中,详见下面的代码。




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [tableData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //从XIB文件中获取模板
    static NSString *CustomCellIdentifier = @"CustomCellIdentifier";
    UINib *nib = [UINib nibWithNibName:@"JXManagerViewCell" bundle:nil];
    [tableView registerNib:nib forCellReuseIdentifier:CustomCellIdentifier];
    
    JXManagerViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
    //重用表格
    if (cell == nil) {
        cell = [[JXManagerViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CustomCellIdentifier];
    }
    
    
    
    
    JXManagerVo *mvtemp =[tableData objectAtIndex:indexPath.row];
    
//    if(mvtemp.isFinishFlag)
//    {
//        mvtemp.leftIcon = [NSString stringWithFormat:@"%@-1",mvtemp.leftIcon];
//    }
    
    NSMutableString *lefticonpath = [NSMutableString stringWithString: [mvtemp leftIcon] ];
    if([mvtemp isFinishFlag])
    {
        [lefticonpath appendString:@"-1"];
    }
    NSString *righticonpath =[mvtemp rightIcon];
    
    //绑定数据
    
    cell.leftIcon =[UIImage imageNamed:lefticonpath];
    cell.rightIcon = [UIImage imageNamed:righticonpath];
    cell.subject = [mvtemp subject];
    cell.content = [mvtemp content];
    cell.floatDetailContent = [mvtemp floatDetailContent];
    cell.rightIconTip  = [mvtemp rightIconTip];
    cell.busId= [mvtemp busId];
    cell.hiddenViewArray = [mvtemp.dic objectForKey:@"history"];
    
    
    
    //设置隐藏内容可见性
    if(mvtemp.isShowHiddenDetail)
    {
        cell.hiddenview.hidden=false;
        
    }else{
        cell.hiddenview.hidden=true;
    }
    cell.delegate = self;//给cell设置代理
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    tableView.delegate=nil;
    tableView.delegate=self;
    
    JXManagerVo *mvtemp =[tableData objectAtIndex:indexPath.row];
    int cellHeight =[mvtemp.cellHeight intValue];
    return cellHeight;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"关键动作执行";
    
}


#pragma mark - delegate

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //处理单击操作
    
    JXManagerViewCell *tableViewCell = (JXManagerViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    
    
    NSInteger sections = tableView.numberOfSections;
    
    for (int section = 0; section < sections; section++) {
        NSInteger rows =  [tableView numberOfRowsInSection:section];
        for (int row = 0; row < rows; row++) {
            NSIndexPath *inpath = [NSIndexPath indexPathForRow:row inSection:section];
            
            if(indexPath.row==inpath.row && indexPath.section==inpath.section)
            {
                JXManagerVo *mvtemp =[tableData objectAtIndex:inpath.row];
                if(mvtemp.isShowHiddenDetail)
                {
                    mvtemp.isShowHiddenDetail=false;
                    mvtemp.cellHeight = [[NSNumber alloc] initWithInt:MAIN_CELLS_HEIGHT];
                }
                else{
                    mvtemp.isShowHiddenDetail=true;
                    mvtemp.cellHeight = [[NSNumber alloc] initWithInt:MAIN_CELLS_HEIGHT +10+ tableViewCell.hiddenview.frame.size.height ];
                }
                
                
            }
            else{
                JXManagerVo *mvtemp =[tableData objectAtIndex:inpath.row];
                mvtemp.isShowHiddenDetail=false;
                mvtemp.cellHeight = [[NSNumber alloc] initWithInt:MAIN_CELLS_HEIGHT ];
                
            }
            
        }
    }
    [tableView reloadData];
    
    self.tableView.contentSize = CGSizeMake(self.tableView.contentSize.width, self.tableView.contentSize.height+50);

    
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值