husharoonie
husharoonie

Reputation: 441

Hide UIImageView in TableViewCell for Constraint Purposes

I have my tableViewCell set up for

UITableViewAutomaticDimension

However, some cells will not have a UIImage. I would like the cell to adjust size if it doesnt contain an image. How can I set the height of the cell in a proper manner to where i can accomplish this.

enter image description here

I would like to make the cell adjust to the height of the autoDimension of the textView(above the imageView in storyboard) if the image is missing. Whats the best way to go about this?

enter image description here

UPDATE*:

So i did exactly what you said but i did it with a identifier rather than an IBAction.

  if let c = messageImage.constraint(withIdentifier: "imageViewHeightConstraint") {
                    // do stuff with c

                    if(messageArray[indexPath.row].uploadedPhotoUrl == ""){
                        c.constant = 1
                    }else {
                        c.constant = 162
                    }

                    // c.constant =  (messageArray[indexPath.row].uploadedPhotoUrl == "") ? 1 : 162
                }

let filter = AspectScaledToFillSizeFilter(size: messageImage.frame.size)
messageImage.af_setImage(withURL: messageImageDownloadUrl! as URL, filter: filter)

cell?.layoutIfNeeded()
return cell!

UI:

enter image description here

Upvotes: 0

Views: 202

Answers (2)

tereks
tereks

Reputation: 1298

The good way is using horizontal stackview

and when you don't need imageview set imageview.isHidden = true

Upvotes: 0

Shehata Gamal
Shehata Gamal

Reputation: 100503

Hook the height constraint of the UIImageview as IBOutlet and inside cellForRow

cell.imageVHeightCon.constant = ( image == nil ) ? 0 : 200
cell.layoutIfNeeded()
return cell

Upvotes: 2

Related Questions