title: Dynamic TableViewCell height to fit downloaded image
body: <p>I was following this answer: <a href="https://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights">Using Auto Layout in UITableView for dynamic cell layouts &amp; variable row heights</a> but it lacks 2 things that made me unable to complete it:</p>
  
  <ol>
  <li>I set the constraints in the Storyboard prototype cell, not programmatically</li>
  <li>The image download is done async.</li>
  </ol>
  
  <p>Basically I'm downloading an image using <code>SDWebImage</code>, and resizing the <code>UIImageView</code>'s height so that it displays the entire image (scaled down, of course). The problems I'm having are commented in the code below:</p>
  
  <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  
     // dequeue cell using identifier
  
    [cell.pictureView sd_setImageWithUrl:myUrl completed:^( ... block ...) {
  
         // This code will scale down the UIImage to the UIImageView's width maitaining
         // its aspect ratio, and also will resize the UIImageView's to match the scaled-down UIImage's height
  
         if(image != nil) {
           CGSize imageSize = [image size];
           CGSize imageViewSize = CGSizeMake(280.0, 100.0);
  
           CGFloat correctHeight = (imageViewSize.width / imageSize.width) * imageSize.height;
  
           [[cell pictureView] setFrame:CGRectMake([[cell pictureView] frame].origin.x,
                                                    [[cell pictureView] frame].origin.y,
                                                    CGRectGetWidth([[cell pictureView] bounds]),
                                                    correctHeight)];
  
             // At this point, the subviews are all set. Since this happens async, what method do I call to re-calculate this row's height.
  
    }];
  
  
  - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  
      static int defaultHeight = 230;
  
      // Get's the offscreen cell used only for calculation purposes
      WKZEventsCell *cell = [[self offscreenCells] objectForKey:CellIdentifier];
      if(!cell) {
  
          // HOW DO I GET A REFERENCE TO A PROTOTYPED CELL INSIDE THE STORYBOARD??? 
          // note that calling dequeueReusableCellWithIdentifier:CellIdentifier will cause a memory leak      
  
          cell = ??????
          [[self offscreenCells] setObject:cell forKey:CellIdentifier];
      }
  
      WKZEvent *event = [[self eventList] objectAtIndex:[indexPath row]];
      [[cell titleLabel] setText:[event title]];
      [[cell placeLabel] setText:[event title]];
      [[cell pictureView] setImage:image];
  
  
      // Even tho this should be in memory cache right now, I'm pretty sure this call is still asynchronous, so this might make the whole thing wont work.
      [cell.pictureView sd_setImageWithUrl:myUrl
  
  
      // THIS METHODS ARE COMMENTED BECAUSE MY CONSTRAINTS ARE SET IN STORYBOARD, BUT NOT SURE IF I'M RIGHT AT THIS ONE
      //[cell setNeedsUpdateConstraints];
      //[cell updateConstraintsIfNeeded];
  
      // I DONT HAVE A MULTILINE LABEL SO I DONT CALL THIS. NOT SURE ABOUT THIS ONE EITHER
      //cell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(cell.bounds));
  
      [cell setNeedsLayout];
      [cell layoutIfNeeded];
  
      CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
  
      height += 1;
  
      NSLog(@"%f", height);
  
      return height;
  }
  </code></pre>