将UITableView的高度调整为仅满足总内容大小所需的高度 [英] Resize UITableView's height to be as high as it needs to fit only total content size
问题描述
所以我有一个非常基本的问题。我在视图
中有这个 UITableView
,我想让这个表的高度尽可能高显示表中的所有行。因此,我不希望能够滚动,我只想要显示所有行。 (行数量是动态的,高度
)。
So I am having a very basic issue. I have this UITableView
within a view
, and I would like to make this table's height as high as it needs to be to display ALL the rows within the table. Therefore, I don't want to be able to scroll, I just want all the rows to appear. (The rows are dynamic in quantity and height
).
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGSize cellSize = [[[_stepsArray objectAtIndex:indexPath.row]content] sizeWithFont:[UIFont boldSystemFontOfSize:18.0f] constrainedToSize:CGSizeMake(528.0f, CGFLOAT_MAX)lineBreakMode:NSLineBreakByWordWrapping];
return cellSize.height;
}
如果有20行,表格会很高,但如果有的话只有1行,它会非常小,其他内容不会出现在19行以下。
If there are 20 rows, the table will be very high, but if there is only 1 row, it will be very small and the other content will not appear 19 rows below.
推荐答案
如果我理解它正确地说,您应该能够为每一行添加高度并根据以下内容调整tableview高度:
if I understand it correctly, you should be able to add up the height for each row and adjust the tableview height based on that:
CGFloat tableHeight = 0.0f;
for (int i = 0; i < [_stepsArray count]; i ++) {
tableHeight += [self tableView:self.tableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
}
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, self.tableView.frame.size.width, tableHeight);
你可以在[tableView reloadData]
you can do this immediately after [tableView reloadData]
这篇关于将UITableView的高度调整为仅满足总内容大小所需的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!