下载图像上的iOS动态表格视图单元格高度

时间:2014-08-09 06:13:06

标签: ios uitableview

我正在使用SDWebImage库来下载图像,然后使用URL作为密钥将它们缓存在NSMutableDictionary中。我们的想法是调整UIImage的大小(即从2800x2000到280x200),UIImageView的高度将适应它(即高度为200)。因此,细胞高度应分别增长。

heightForRowAtIndexPath:内部我遇到了返回高度错误的问题,这是交易:

- (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;
}

基本上我只是为了测量目的而得到一个单元格实例。打印实际大小和缩放大小的NSLog()调用很好。问题是height始终是1044.0之类的值,它应该返回220左右的某些内容(例如图像高度缩小到200的示例)。

我在某处读到调用dequeueReusabeCellWithIdentifier:会导致内存泄漏,所以这是我的第一个假设,但我不确定。

0 个答案:

没有答案
相关问题