加载单元格后UITableView行高

时间:2016-07-14 10:15:21

标签: ios objective-c uitableview

UITableViewstoryboard中包含4种不同类型的自定义单元格。每个单元格都有自定义UILabels,从后端获取可变数量的文本数据。我正在努力使细胞正确调整大小。我真的想要改变每个单元格的高度,但我不能使用heightForRowAtIndexPath,因为它在cellForRowAtIndexPath之前调用,但实际上是在每个自定义单元格中计算高度。

我尝试在UITableView加载时将每个单元格的高度写入数组,然后再重新加载一次,但没有效果。我尝试使用CGFloat rowHeight = UITableViewAutomaticDimension也没有成功。每个单元格中的自定义标签肯定会随着文本的增长而增长,当我只是将行高度静态更改为更高的数字时,我看到了所以,我需要以某种方式推动我的标签推动行增长,不确定。

我发现stackoverflow上的不同类似帖子没有帮助。

2 个答案:

答案 0 :(得分:0)

问题在于我需要为ContentView设置顶部和底部约束,而不是在故事板中设置单元格本身。

enter image description here

标签 - >需要设置ContentView顶部和底部约束。然后在UITableViewAutomaticDimension中指定viewDidLoad

self.tableView.rowHeight = UITableViewAutomaticDimension;

estimatedRowHeight也应该设置。例如:

self.tableView.estimatedRowHeight = 76.0f;

答案 1 :(得分:0)

第一个叫做的方法是:

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath

第二:

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

然后:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

在上述方法中设置一个断点并进行测试。因此,如果要使用estimatedHeightForRowAtIndexPath方法预设高度。