I am trying to make a UITableViewCell as shown below, which will adjust its height according to its presentation of the content. The image in the center should be as wide as the frame of the cell, and proportionally adjust its height.

As you can see, the image in the cell does not display the way I want. I tried to set the height from UIImageView to 320.0 in override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell , but it doesn't help at all.
Question: how can I get a cell to respect image height?
FYI:
- I do not override
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat - I do not set a height limit on
UIImageView or anything else, I use paddings only to organize the views. - The image is displayed using the approximation mode.
Edit:
I moved on to add a height limit for the image:
class MyTableViewPostCell: UITableViewCell { @IBOutlet weak var contentLabel: UILabel! @IBOutlet weak var pictureImageView: UIImageView! @IBOutlet weak var pictureImageViewHeightConstraint: NSLayoutConstraint! } class MyTableViewController: UITableViewController { override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 1 } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("MyTableViewPostCell") as MyTableViewPostCell cell.contentLabel.text = "This is a very very very very very very very long content." cell.pictureImageViewHeightConstraint.constant = 320 cell.pictureImageView.sd_setImageWithURL(NSURL(string: "http://somedomain.com/pic")) return cell } }
And when a constraint conflict occurs at startup. See below:
( "<NSLayoutConstraint:0x7ff7b5026c30 V:[UIImageView:0x7ff7b50289e0(40)]>", "<NSLayoutConstraint:0x7ff7b5029a90 V:[UIImageView:0x7ff7b5029990(320)]>", "<NSLayoutConstraint:0x7ff7b502a630 UIImageView:0x7ff7b50289e0.top == UITableViewCellContentView:0x7ff7b5028910.topMargin + 6>", "<NSLayoutConstraint:0x7ff7b502a770 V:[UIImageView:0x7ff7b50289e0]-(12)-[UILabel:0x7ff7b5029390'This is a very very very ...']>", "<NSLayoutConstraint:0x7ff7b502a950 V:[UILabel:0x7ff7b5029390'This is a very very very ...']-(10)-[UIImageView:0x7ff7b5029990]>", "<NSLayoutConstraint:0x7ff7b502aa40 UITableViewCellContentView:0x7ff7b5028910.bottomMargin == UILabel:0x7ff7b5029c20'Label'.bottom + 10>", "<NSLayoutConstraint:0x7ff7b502aa90 V:[UIImageView:0x7ff7b5029990]-(17)-[UILabel:0x7ff7b5029c20'Label']>", "<NSLayoutConstraint:0x7ff7b2cae430 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7ff7b5028910(283.5)]>" ) Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7ff7b5029a90 V:[UIImageView:0x7ff7b5029990(320)]>
I don't know where the UIView-Encapsulated-Layout-Height comes from, but it looks like this is causing a conflict. Any way to fix this? Thanks!
source share