动态调整UITableView高度 [英] Adjust UITableView height dynamically

查看:93
本文介绍了动态调整UITableView高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何动态调整UITableView高度?假设我有3行,每行的高度为65.将新行添加到UITableView时,我希望它动态调整其高度以适应新行.反之亦然,当删除一行以缩小时.

How can I adjust the UITableView height dynamically? Let's say I have 3 rows and each row has a height of 65. When a new row is added to the UITableView i want it to adjust it's height dynamically to fit the new row. And vice versa when a row is deleted to shrink.

推荐答案

误读

事情取决于(table cell.h.表的行数*表中的行数)作为tableview.frame.size.height.

The thing depends on the (height of table cell row * number of rows in table) as the tableview.frame.size.height.

好吧,我建议像这样.

-(void)correctHeight
{

    //Note : if all cells are having common value
    CGFloat heightOfCell=[table rowHeight];

    CGFloat expectedHeightOfTable=0;
    for (int i=0; i< [table numberOfSections]; i++) {
        //header section
        CGFloat heightOfHeaderView=[table headerViewForSection:i].frame.size.height;
        expectedHeightOfTable = expectedHeightOfTable +heightOfHeaderView;

        //content section
         expectedHeightOfTable = expectedHeightOfTable + heightOfCell*[table numberOfRowsInSection:i];
    }
    [table setFrame:CGRectMake(table.frame.origin.x, table.frame.origin.y, table.frame.size.width,expectedHeightOfTable)];


}

这将考虑标题和每一行的内容以及按比例填充的所有值.因此不需要添加一些硬编码的值

This will consider header and each row content and all value populated prormatically..So dont need to add some hardcoded values

这篇关于动态调整UITableView高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆