2017-02-15 69 views
0

我有一个自己的尺寸UITableViewCell显示博客文章。
一篇文章由文字图片组成。我现在面临的挑战是有些帖子不包含图片。根据内容的可用性调整UITableViewCell的尺寸

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

谢谢!

回答

2

考虑你说你正在使用自动调整大小的细胞,下面是步骤,您应该遵循:

1)首先子类的tableview cell定制UITableViewCell

2)采取上述类的UIImageView的出口为:

@IBOutlet weak var myImageView: UIImageView 

3)设置auto layout约束从故事板此图像视图leadingtrailingtopheight约束

4)取出口,用于在相同的UITableViewCell类的ImageView的height constraint

enter image description here

@IBOutlet weak var heightConstraint: NSLayoutConstraint! 

5)在你的cellForRow方法,检查是否数据中包含的图像或没有。如果没有图像,则更改高度约束的值:

if myImage == nil { 

    if myCustomCell.heightConstraint != nil { 

     myCustomCell.heightConstraint.constant = 0 
     myCustomCell.updateConstraintsIfNeeded() 
    } 
} 

这应该按预期工作。如果不是,请张贴您已经申请的限制条件。

+0

这完美@Nilesh波尔。非常感谢! –