所以我遇到了一个非常基本的问题。我在 view
中有这个 UITableView
,我想让这个表的高度达到显示表中所有行所需的高度。因此,我不希望能够滚动,我只想显示所有行。 (行的数量和高度
是动态的)。
-(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行下面。
最佳答案
如果我理解正确的话,你应该能够将每一行的高度加起来并根据它调整 tableview 的高度:
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] 之后立即执行此操作
关于ios - 将 UITableView 的高度调整为仅适合总内容大小所需的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13574071/