title: Dynamic cell height with SDWebImage
body: <p>I have a table view and table view cells with an image view on them. I want them to have a fixed width but with a dynamic height (depending on the image coming in from the server). </p>
  
  <p>I am using <code>SDWebImage</code> to download and set the image, but the table view cells are turning out very weird.</p>
  
  <p>I didn't forget to:</p>
  
  <pre><code>postTableView.estimatedRowHeight = UITableViewAutomaticDimension
  postTableView.rowHeight = UITableViewAutomaticDimension
  </code></pre>
  
  <p>cellForRow method:</p>
  
  <pre><code>func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
      let cell = tableView.dequeueReusableCell(withIdentifier: "postCell", for: indexPath) as! TableViewCell
      let post = postArray[indexPath.row]
      cell.setupCell(with: post)
      return cell
  }
  </code></pre>
  
  <p>Table view cell class:</p>
  
  <pre><code>class TableViewCell: UITableViewCell {
  
      @IBOutlet weak var postTitle: UILabel!
      @IBOutlet weak var postSource: UILabel!
      @IBOutlet weak var postChart: UIImageView!
  
      internal var aspectConstraint: NSLayoutConstraint? {
          didSet {
              if oldValue != nil {
                  postChart.removeConstraint(oldValue!)
              }
              if aspectConstraint != nil {
                  postChart.addConstraint(aspectConstraint!)
              }
          }
      }
  
      override func awakeFromNib() {
          super.awakeFromNib()
          selectionStyle = UITableViewCellSelectionStyle.none
      }
  
      override func prepareForReuse() {
          super.prepareForReuse()
          postChart.image = nil
          aspectConstraint = nil
      }
  
      func setupCell(with post: Post) {
          postTitle.text = post.title
          postSource.text = post.source
          let tempImageView = UIImageView()
          tempImageView.sd_setImage(with: URL(string: post.chartURL!), placeholderImage: UIImage(named: "placeholder.png")) { (image, error, cache, url) in
              if let image = image {
                  self.setCustomImage(image: image)
              }
          }
      }
  
      func setCustomImage(image: UIImage) {
          let aspect = image.size.width / image.size.height
          let constraint = NSLayoutConstraint(item: postChart, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: postChart, attribute: NSLayoutAttribute.height, multiplier: aspect, constant: 0.0)
          constraint.priority = UILayoutPriority(rawValue: 999)
          aspectConstraint = constraint
          postChart.image = image
          setNeedsLayout()
      }
  }
  </code></pre>