使用可选图像(Swift)调整UITableViewCell大小

时间:2022-11-13 10:53:08

I am having a doozy of a time trying to figure this one out:

我有一段时间试图弄清楚这个:

I have a UITableViewCell with a fixed sized UIImage (250 x 250) pinned to the top of the cell's View and a UILabel of variable text size underneath with its top pinned to the bottom of the UIImage and its bottom to the bottom of the cell's View.

我有一个UITableViewCell,固定大小的UIImage(250 x 250)固定在单元格View的顶部,下面是可变文本大小的UILabel,顶部固定在UIImage的底部,底部固定在单元格的底部。

I have a use case where an image is not always guaranteed and so the image view purposely collapses to 0 height so it does not break autolayout on the UILabel's top pinning.

我有一个用例,其中图像并不总是得到保证,因此图像视图故意折叠到0高度,因此它不会破坏UILabel顶部钉扎的自动布局。

I assumed that this would allow the UITableViewCell to shrink down to the size of the label but instead, it stays the default height and autolayout stretches the label's height (to maintain the pinning to the top and the bottom) with the text centered in the middle.

我假设这将允许UITableViewCell缩小到标签的大小,但相反,它保持默认高度,autolayout拉伸标签的高度(以保持固定到顶部和底部),文本居中于中间。

I used all the usual suspects, UITAbleViewAutomaticDimension and estimatedRowHeight blah blah (what you would probably suggest first!) and it was to no avail.

我使用了所有常见的嫌疑人,UITAbleViewAutomaticDimension和estimatedRowHeight等等等等(你可能会先建议!)并且无济于事。

What am I doing wrong?

我究竟做错了什么?

I did see something about changing the Priority for the constraints in IB to 250 instead of 1000 so they could break. And also some references to resizing the UITableViewCell manually (lame and nasty) by calling requiredHeight() on the UILabel which broke the layout, so what am I doing wrong here? Thanks!

我确实看到了一些关于将IB中的约束优先级更改为250而不是1000的内容,因此它们可能会中断。还有一些参考通过在UILabel上调用requiredHeight()来手动调整UITableViewCell(跛脚和讨厌),这打破了布局,所以我在这里做错了什么?谢谢!

1 个解决方案

#1


2  

I have a use case where an image is not always guaranteed

我有一个用例,其中不总是保证图像

For this situation you can create 2 type of UITableViewCell: the first one with UIImageView and the second without it. Then, in cellForRowAtIndexPath init the cell you want.

对于这种情况,您可以创建2种类型的UITableViewCell:第一种使用UIImageView,第二种不使用UIImageView。然后,在cellForRowAtIndexPath中初始化您想要的单元格。

#1


2  

I have a use case where an image is not always guaranteed

我有一个用例,其中不总是保证图像

For this situation you can create 2 type of UITableViewCell: the first one with UIImageView and the second without it. Then, in cellForRowAtIndexPath init the cell you want.

对于这种情况,您可以创建2种类型的UITableViewCell:第一种使用UIImageView,第二种不使用UIImageView。然后,在cellForRowAtIndexPath中初始化您想要的单元格。