Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 动态调整UITableView高度_Ios_Iphone_Uitableview_Autolayout - Fatal编程技术网

Ios 动态调整UITableView高度

Ios 动态调整UITableView高度,ios,iphone,uitableview,autolayout,Ios,Iphone,Uitableview,Autolayout,如何动态调整UITableView高度?假设我有3行,每行的高度是65。将新行添加到UITableView时,我希望它动态调整其高度以适应新行。当一行被删除以缩小时,反之亦然。编辑:误读 这取决于(表格单元格行的高度*表格中的行数)作为tableview.frame.size.height 好吧,我建议你做这样的 -(void)correctHeight { //Note : if all cells are having common value CGFloat height

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

编辑:误读

这取决于(表格单元格行的高度*表格中的行数)作为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)];


}
—(空)校正高度
{
//注意:如果所有单元格都具有公共值
CGFloat heightOfCell=[表格行高];
CGFloat预期高度OFTABLE=0;
对于(int i=0;i<[表编号部分];i++){
//标题部分
CGFloat heightOfHeaderView=[table headerViewForSection:i].frame.size.height;
expectedHeightOfTable=expectedHeightOfTable+headerview高度;
//内容部分
expectedHeightOfTable=expectedHeightOfTable+单元格高度*[节中的表行数:i];
}
[table setFrame:CGRectMake(table.frame.origin.x,table.frame.origin.y,table.frame.size.width,expectedHeightOfTable)];
}

这将考虑页眉和每行内容,并且所有值都按比例填充。所以不需要添加一些硬编码值

,TabVIEW高度可以由

设置。
[tableView setFrame:CGRectMake(0,0,width,height)];

但是,例如,如果您使用tableView:heightForRowAtIndexPath:将高度设置为150,并将5行高度设置为60px,那么tableView将成为可滚动的,因为UITableView是UIScrollView的一个子类。

如注释中所述,结构怪异,但如果您坚持,这没关系。我建议只使用简单的
UITableView

假设您有
IBOutlet
UITableView
UIScrollView

视图中添加代码之前,请添加:

float cellHeight = 65.0f;
int numberOfCells = 3;
-(void)视图将显示:动画
,在
[超级视图将显示:动画]
之后添加以下内容:

// remember fill the table with data using UITableViewDataSource's functions first
// refresh UITableView data
[tableView reloadData];
// resize UITableView
[tableView setFrame:CGRectMake(0, 0, tableView.frame.size.width, cellHeight * numberOfCells)];
// make UIScrollView the same size as UITableView
[scrollView setContentSize:tableView.frame.size];
[scrollView setNeedsDisplay];
// remember to lock tableView's scrolling, either in code / Interface Builder
[tableView setScrollEnabled:NO];
UITableViewDelegate
的委托方法中:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return numberOfCells;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   return cellHeight;
}
最后,如果要双重验证是否有任何表格单元格不可见,可以使用:

- (NSArray *)visibleCells

你试过什么了吗?我试过了:但太复杂了,一点也不明显。请出示你的代码。@Ukasztomazewski不是重复的。单元格高度与表格高度所有人都急于给出答案,而没有阅读整个问题。OP要求更改UITableView的高度谢谢我尝试过,在调试过程中,表格视图的高度正在更改,但从视觉上看,它始终是相同的高度,因为它的supeview保持相同的标题高度。是的,如果标题设置为可见(因为在大多数情况下标题是隐藏的)。真的取决于OP的布局。谢谢你,我试过了,在调试过程中,tableview的高度会发生变化,但从视觉上看,它的高度总是一样的,我不好。查找文档后,应将代码放在
视图中,以动态更改表视图的大小。视图将显示:动画
,而不是
viewDidLoad