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.