0

I'm using SDWebImage library to download images, then I cache them in an NSMutableDictionary using the URL as the key. The idea is that the UIImage is resized (i.e from 2800x2000, to 280x200) and the UIImageView's height will adapt to it (i.e. height would be 200). Ergo, the cell height should grow respectively.

Inside heightForRowAtIndexPath: I'm having an issue where the returned height is wrong, here's the deal:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

static int defaultHeight = 230;

UIImage *image = (UIImage*) [[self images] objectForKey:[NSNumber numberWithInt:[indexPath row]]];
if(!image) {
    NSLog(@"Null returned");
    return defaultHeight;
}
else {
    NSLog(@"Not null");
    WKZEventsCell *cell = [[self offscreenCells] objectForKey:CellIdentifier];
    if(!cell) {
        NSLog(@"DEQUEUED");
        cell = [[self tableView] dequeueReusableCellWithIdentifier:CellIdentifier];
        [[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];

    [cell setNeedsLayout];
    [cell layoutIfNeeded];


    CGSize imageSize = [image size];
    CGFloat imageViewWidth = CGRectGetWidth([[cell pictureView] bounds]);

    CGFloat correctHeight = (imageViewWidth / imageSize.width) * imageSize.height;

    [[cell pictureView] setFrame:CGRectMake([[cell pictureView] frame].origin.x,
                                            [[cell pictureView] frame].origin.y,
                                            imageViewWidth,
                                            correctHeight)];

    NSLog(@"Real: W: %f, H: %f", imageSize.width, imageSize.height);
    NSLog(@"Scaled: W: %f, H: %f", imageViewWidth, correctHeight);
    NSLog(@"Url: %@", [event image]);


    [cell setNeedsLayout];
    [cell layoutIfNeeded];


    CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

    height += 1.0f;

    NSLog(@"Height: %f", height);

    return height;
}

Basically I'm getting a cell instance for measuring purposes only. The NSLog() calls that prints the real size and scaled size, are fine. The issue is that the height is always a value like 1044.0 where it should be returning something around 220 (for the example where the image height is scaled down to 200).

I read somewhere that calling dequeueReusabeCellWithIdentifier: will result in a memory leak, so that's my first hypothesis but I'm not sure.

Christopher Francisco
  • 15,672
  • 28
  • 94
  • 206
  • Don't use `dequeueReusableCellWithIdentifier` in `heightForRowAtIndexPath`, you should only use image.size calculate cell's height. – KudoCC Aug 09 '14 at 07:29
  • what are the constraints on your cell? – oiledCode Aug 09 '14 at 07:41
  • You must be getting warnings with that code. When you fix your problem, come over and post your code at Code Review, we'd help you write better code. – duci9y Aug 09 '14 at 07:42
  • @KudoCC But the cell's height depends also on another 2 labels, besides de image view. how do I calculate it? – Christopher Francisco Aug 09 '14 at 15:49
  • @elio.d the constraints are similar to the accepted answer in here http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights, the difference would be that in my case, the height depend on the image – Christopher Francisco Aug 09 '14 at 15:50
  • @duci9y well the only warning I have it's about not having an identifier for the segues initiated directly from view controllers – Christopher Francisco Aug 09 '14 at 15:50

0 Answers0