根据下载的图像高度调整表格单元格的高度

时间:2012-04-10 16:07:57

标签: iphone ios uitableview uiimage

我在运行时获取图片的网址,我想下载&在表格中显示这些图像。图像将以异步方式下载。更重要的是,我希望以实际尺寸显示所有这些图像。

请帮助我。在此先感谢:)

6 个答案:

答案 0 :(得分:3)

在委托方法中,您必须在完成下载后更新图像,您可以使用

[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] 
                 withRowAnimation:UITableViewRowAnimationAutomatic];

这将再次致电

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

所以你应该使用图像的高度(如果存在),默认高度或占位符图像(如果有的话)。

答案 1 :(得分:3)

首先,我强烈建议您使用SDWebImage进行异步下载,如果您还没有这样做的话。根据您的需要,您可以将其缓存在内存或磁盘上 - 后者通过将它们保存在特殊缓存文件夹中以正确的方式完成,因此iOS可以在空间不足时删除它们。它还在UIImageView上提供了一个方便的类别,类似于AFNetworking为您提供的类别,但具有更多选项和可选的完成块。这样你就可以做到这样的事情:

#import <SDWebImage/UIImageView+WebCache.h>

...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // Create the cell...

    [cell.myImageView setImageWithURL:[NSURL URLWithString:@"http://example.com/image.jpg"]
              placeholderImage:[UIImage imageNamed:@"placeholder"]
                     completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
        if (image != nil) {
            [self.images insertObject:image atIndex:indexPath.row];
            [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:YES];
        }
    }];
    return cell;
}

这样您可以异步下载图像,正确设置UIImageView,但您也可以使用块将图像保存在NSMutableArray中,这样您就可以告诉tableview正确的高度。它还告诉表视图更新其图像已加载的单元格的高度。然后,当表视图需要知道给定单元格的高度时,如果图像已经加载,我们将从中获取大小:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    id image = [self.images objectAtIndex:indexPath.row];
    if ([image isKindOfClass:[NSNull class]]) {
        return defaultHeight;
    } else {
        return [image size].height + topPadding + bottomPadding;
    }
}

这样我们就知道屏幕外的图像的高度(例如,当你滚动到底部时)。此解决方案假设您首先使用NSNull值填充NSMutableArray,然后在加载时将其替换为图像。最后,如果您愿意,您甚至可以在显示单元格之前使用SDWebImageManager手动启动下载。

答案 2 :(得分:3)

您可以按照下载图片制作UIImageView尺寸,但会看起来很糟糕

因此,我建议你制作相同尺寸的所有图像,这样看起来会很好看,而且很好看

您可以使用它来制作相同尺寸的所有图像。

+ (UIImage *)imageScaledToSizeWithImage:(UIImage *)imagewww andsizeee:(CGSize)size
{
    //avoid redundant drawing
    if (CGSizeEqualToSize(imagewww.size, size))
    {
        return imagewww;
    }

    //create drawing context
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);

    //draw
    [imagewww drawInRect:CGRectMake(0.0f, 0.0f, size.width, size.height)];

    //capture resultant image
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    //return image
    return image;
}

但是如果你真的想要显示不同行大小的表,那么在运行时更改你的行大小

当你得到图像然后将你的图像保存在字典中,键值是行的indexPath。

[tableImageDict setObject:image forKey:[NSString stringWithFormat:@"%i,%i",indexPath.row,indexPath.section]];

然后重新加载表格行。

[table reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                     withRowAnimation:UITableViewRowAnimationNone];

它会改变你的行高

-(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;
    }
    else{
        return 44.0;
    }
}

答案 3 :(得分:2)

结帐heightForRowAtIndexPath:。这是关于它的另一个post

答案 4 :(得分:1)

为此你必须创建自定义UITableviewCell,你可以使用这段代码设置表格视图单元格的大小

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   int rowHeight =0.0f;

   // here u can get particular Url by indexPath.row, now create UIImage object using that Url 

   CGRect bioHeadingFrame = image.frame;
   rowHeight = size.height; //get the height of that image 

   return rowHeight;
}

现在你的桌子的高度根据图像高度增加

答案 5 :(得分:0)

如果异步下载是您的真正问题,请参阅AFNetworking库的UIImageView类别。特别是这个功能:

- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholderImage

您可以将此UIimageview类别包含到项目中,并设置从该类派生的单元格图像。