下载图像时调整UITableViewCell高度

我正在使用UIImageView+AFNetworking类别asynchronous图像加载。 一切工作正常,但我已经尝试了几件事情,并根据下载的图像调整单元格的高度不成功。 我想图像适合单元格的宽度,但调整高度,但我不知道如何做到这一点。 我已经尝试重新加载图像下载的行,但只是导致cellForRowAtIndexPath再次启动,并再次设置一切,等等几乎是一个recursion。

我正在计算新的图像大小差异,并将其存储在NSMutableArray ,然后重新加载在UIImageView的setImageWithURLRequest:placeholderImage:success:成功块中的行setImageWithURLRequest:placeholderImage:success:

我在heightForRowAtIndexPath得到了几行的正确高度,但是,表开始变得怪异,一切都在叠加等等。

有任何想法吗?

谢谢!

编辑

我最终使用了Ezeki的答案。 我也写了一个关于这个话题的post,并且使用这个技术做了一个示例应用程序。

看看这里 。

您需要将下载的图像存储在内存中或磁盘上,因此下次您将尝试从此URL获取图像时,您将从caching中收到图像。

所以,如果你这样做,你将不得不这样做:

 [tableView beginUpdates]; [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; [tableView endUpdates]; 

你应该在这个表格视图数据源的方法中返回新的单元格的高度:

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

我会build议你使用SDWebImage库,而不是AFNetworking因为它可以caching你的图像memcache和磁盘为你,这是非常容易使用。 所以,如果你决定使用它,下载图像的代码将如下所示:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ... [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"] success:^(UIImage *image, BOOL cached) { // save height of an image to some cache [self.heightsCache setObject:[NSNumber numberWithFloat:imHeight] forKey:urlKey]; [tableView beginUpdates]; [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; [tableView endUpdates]; } failure:^(NSError *error) {... failure code here ...}]; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { // try to get image height from your own heights cache // if its is not there return default one CGFloat height = [[self.heightsCache objectForKey:urlKeyFromModelsArrayForThisCell] floatValue]; ... return newHeight; } 

对我来说这个工作:

您检查caching映像是否可用,如果可用,则将其设置为映像,如果不可用,则将其下载,然后通过调用reload索引path重置表的布局。

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *broadcastTableCellIdentifier = @"BlockbusterTableViewCell"; BlockbusterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:broadcastTableCellIdentifier]; if (cell == nil) { cell = [[BlockbusterTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:broadcastTableCellIdentifier]; } [self configureCell:cell atIndexPath:indexPath]; return cell; } -(void)configureCell:(BlockbusterTableViewCell*)cell atIndexPath:(NSIndexPath*)indexPath{ SportData *data = [self.webShared.arrSportsData objectAtIndex:self.selectedEvent]; BroadcastData *broadcastData = [data.broadcastData objectAtIndex:indexPath.row]; NSURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:broadcastData.image]]; AFImageDownloader *downloader = [UIImageView sharedImageDownloader]; id <AFImageRequestCache> imageCache = downloader.imageCache; UIImage *cacheimage = [imageCache imageforRequest:urlRequest withAdditionalIdentifier:nil]; if (cell.imgBroadcast.image != cacheimage) { [cell.imgBroadcast setImageWithURLRequest:urlRequest placeholderImage:[UIImage imageNamed:@"placeholder_smaller"] success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull image) { cell.imgBroadcast.image = image; [self.tableViewBroadcast beginUpdates]; [self.tableViewBroadcast reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; [self.tableViewBroadcast endUpdates]; } failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) { }]; } else{ cell.imgBroadcast.image =cacheimage; } cell.lblTitle.text = broadcastData.title; cell.lblText.text = broadcastData.text; cell.lblEventDateTime.text = [self eventTime:broadcastData.eventTime]; }