根据响应中的内容可用性调整UITableViewCell的大小

时间:2017-02-15 16:27:34

标签: ios swift uitableview autolayout constraints

我有一个自我调整 UITableViewCell ,可以显示博客帖子 帖子由文字图片组成。我现在面临的挑战是某些帖子不包含图片。

如何使用自动布局调整单元格的大小?也许我应该设置UIImageView.isHidden = true而不是?

谢谢!

1 个答案:

答案 0 :(得分:2)

考虑到您说您正在使用自动调整大小的单元格,以下是您应该遵循的步骤:

1)首先使用自定义UITableViewCell

继承您的@IBOutlet weak var myImageView: UIImageView

2)在上面的课程中选择UIImageView的出口:

auto layout

3)将leading约束从故事板设置为此图片视图为trailingtopheightheight constraint约束

4)在同一个UITableViewCell类中获取@IBOutlet weak var heightConstraint: NSLayoutConstraint! imageview的出口:

enter image description here

cellForRow

5)在if myImage == nil { if myCustomCell.heightConstraint != nil { myCustomCell.heightConstraint.constant = 0 myCustomCell.updateConstraintsIfNeeded() } } 方法中,检查您的数据是否包含图片。如果没有图像,则更改高度约束的值:

enzyme

这应该按预期工作。如果没有,请发布您已应用的约束。