关于IOS UITableView的动态高度问题

来源:互联网 发布:淘宝销量少的可以买吗 编辑:程序博客网 时间:2024/05/18 16:17

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);    }




0 0
原创粉丝点击