在tableview中我有4个静态单元格。如何自动计算行高以适应内部和一个手动标签

时间:2023-02-09 21:14:33

So i have four sections and one cell in each one of the sections in my static tableview. One of them is displaying a picture and for that one i manually calculate the ration and multiply it by the width and that way i get the correct height. I do that using the "heightForRowAtIndexPath".

所以我在静态tableview中的每个部分都有四个部分和一个单元格。其中一个是显示图片,为此我手动计算配给量并将其乘以宽度,这样我得到正确的高度。我使用“heightForRowAtIndexPath”来做到这一点。

I also have three other cells and each one has a label in it. The labels content is different each time since i'm segueing to it from a different cell so sometimes the cell should be big enough for 1 row of text and sometimes for more. How do i calculate/set that to happen automatically?

我还有三个其他单元格,每个单元格中都有一个标签。标签内容每次都不同,因为我从不同的单元格中划分它,所以有时候单元格应该足够大,可以容纳1行文本,有时甚至更多。我如何计算/设置自动发生?

Also in my storyboard i have the constrains for the label set to be 8 point away from the right left and top, thats all the constrains that are on the label. I already tried setting the tableView.rowHeight to UITableViewAutomaticDimensions but that doesn't do anything. The rows just stay at the same height as they were set in the "heightForRowAtIndexPath" or the same as in the storyboard if that function is not implemented. Been trying to solve that for probably more then an hour now and still cant figure it out. Thanks for the help. Also i'm doing all of this in swift.

同样在我的故事板中,我有标签集的约束距离左右顶部8点,这就是标签上的所有约束。我已经尝试将tableView.rowHeight设置为UITableViewAutomaticDimensions但是没有做任何事情。行只是保持与“heightForRowAtIndexPath”中设置的高度相同,或者与故事板中的行相同(如果未实现该功能)。一直试图解决这个问题大概超过一个小时,但仍然无法弄明白。谢谢您的帮助。我也在迅速做所有这些。

1 个解决方案

#1


0  

If you implement heightForRowAtIndexPath it will also override any value for the table view's rowHeight. So, in heightForRowAtIndexPath return UITableViewAutomaticDimension, and do the same in estimatedHeightForRowAtIndexPath too.

如果实现heightForRowAtIndexPath,它还将覆盖表视图的rowHeight的任何值。因此,在heightForRowAtIndexPath中返回UITableViewAutomaticDimension,并在estimatedHeightForRowAtIndexPath中也一样。

As long as your cell has a solid Auto Layout configuration, that's all it takes to make auto-sizing cells work.

只要您的单元格具有可靠的自动布局配置,就可以使自动调整大小单元工作。

#1


0  

If you implement heightForRowAtIndexPath it will also override any value for the table view's rowHeight. So, in heightForRowAtIndexPath return UITableViewAutomaticDimension, and do the same in estimatedHeightForRowAtIndexPath too.

如果实现heightForRowAtIndexPath,它还将覆盖表视图的rowHeight的任何值。因此,在heightForRowAtIndexPath中返回UITableViewAutomaticDimension,并在estimatedHeightForRowAtIndexPath中也一样。

As long as your cell has a solid Auto Layout configuration, that's all it takes to make auto-sizing cells work.

只要您的单元格具有可靠的自动布局配置,就可以使自动调整大小单元工作。