iosuitableviewuitableviewautomaticdimension

UITableView dynamic cell height conflict - height NOT depending on a label


So, my table view displays images. Every cell is basically an image filling out the cells entire contentView. As the images come with different aspect ratios, I need my cells to be adjusting their height depending on the table views width and the aspect ratio of the image. I've followed this Ray Wenderlich tutorial but now get a constraint conflict. The image is resized by altering the imageViews height constraint e.g. myImageViewHeight.constant = tableView.frame.width / aspectRatio

2016-06-16 13:56:25.823 MyApp[92709:5649464] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. 
Try this: 
    (1) look at each constraint and try to figure out which you don't expect; 
    (2) find the code that added the unwanted constraint or constraints and fix it. 
(
"<NSLayoutConstraint:0x7fbcdaf5c190 V:[UIImageView:0x7fbcdaf5c300(303)]>",
"<NSLayoutConstraint:0x7fbcdaf5b460 V:|-(0)-[UIImageView:0x7fbcdaf5c300]   (Names: '|':UITableViewCellContentView:0x7fbcdaf52230 )>",
"<NSLayoutConstraint:0x7fbcdaf5b4b0 V:[UIImageView:0x7fbcdaf5c300]-(0)-|   (Names: '|':UITableViewCellContentView:0x7fbcdaf52230 )>",
"<NSLayoutConstraint:0x7fbcdaf5e550 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7fbcdaf52230(100)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7fbcdaf5c190 V:[UIImageView:0x7fbcdaf5c300(303)]>

The image view has the following constraints and has the cells contentView as superview.

Constraints

In the table view controller class, I'm using

self.tableView.estimatedRowHeight = 80
self.tableView.rowHeight = UITableViewAutomaticDimension

I've also tried this - same result.

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

I'm guessing that I have to get rid of the 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7fbcdaf52230(100)]>" but how? Also, does anybody know some kind of documentation/tutorials on how to properly create table views with dynamic content that not only cover having a bunch of labels inside the cell? My code works just fine if the image view is replaced by a label...


Solution

  • You implementation is correct don't change anything in your code or any of your constraints.Removing the warning is easy just select the height constraint and lower its priority from 1000 to 999 it will fix your warning.If it still comes, let me know.