0

How can I adjust a UITableViewCell's height dynamically based on the height of an image downloaded from an API?

I tried to use tableView:cellForRowAtIndexPath:indexPath but failed.

FYI, I am using AFNetworking to get the images' url from api and I use SDWebImage to get the image data.

Steffen D. Sommer
  • 2,896
  • 2
  • 24
  • 47
Yin2Chiu
  • 9
  • 2
  • possible duplicate of [Resize the Table cell height as per downloaded image height](http://stackoverflow.com/questions/10092340/resize-the-table-cell-height-as-per-downloaded-image-height) – Adam Jenkins Jun 30 '15 at 13:30

1 Answers1

0

Set height from heightForRowAtIndexPath method.

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIImage *image = (UIImage *)[tableImageDict objectForKey:
                                 [NSString stringWithFormat:@"%i,%i",
                                  indexPath.row,indexPath.section]];


    if (image != nil)
    {
        return image.size.height; // you can set your size by taking from sdwebimage
    }
    else{
        return 44.0;
    }
}

See the answer related to it.

Community
  • 1
  • 1
Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136