Yin2Chiu
Yin2Chiu

Reputation: 9

iOS7 Adjust TableViewCell's height dynamically based on the height of image downloaded from API

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.

Upvotes: 0

Views: 343

Answers (1)

Ashish Kakkad
Ashish Kakkad

Reputation: 23882

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.

Upvotes: 0

Related Questions