2017-03-08 136 views
1

我想在下載圖像後調整imageview'height的大小,但是在第一次加載tableview時發生了第一個單元格圖像的錯誤。Objective-C:自定義表格視圖單元格下載後動態圖像的高度

un normal cell

然後,如果我滾動表,使細胞處於可見再次,它去是正常的,因爲第二圖像;

normal cell

我不知道圖像的大小,無法從服務器獲取,如何解決呢?謝謝!

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    ShopingTableCell* cell = [tableView dequeueReusableCellWithIdentifier:@"shopingCell" forIndexPath:indexPath]; 
    StoreDynamicsModel *model = storeArray[indexPath.row]; 
    cell.contentLabel.text = model.name; 
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:model.goods_img_url] placeholderImage:[UIImage imageNamed:@"defaultShort"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { 

     dispatch_async(dispatch_get_main_queue(), ^{ 

      cell.imgViewHeightConstraint.constant = image.size.height * ScreenWidth/image.size.width; 
     }); 
    }]; 

    return cell; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    tableView.rowHeight = UITableViewAutomaticDimension; 
    tableView.estimatedRowHeight = 120.f; 
    return self.tableView.rowHeight; 
} 

回答

1

您可以將內容模式UIImageView設置爲Aspect Fit。

cell.imageView.contentMode = UIViewContentModeScaleAspectFit; 

對於參考:

How to manage UIImageView content mode?

+0

'[tableview中beginUpdates]; [tableView endUpdates];'會強制桌面視圖以動畫方式刷新高度。下面的代碼工作 'if(cacheType == SDImageCacheTypeNone || cacheType == SDImageCacheTypeDisk){[tableView beginUpdates]; imgViewHeightConstraint.constant = image.size.height * ScreenWidth/image.size.width; [tableView reloadRowsAtIndexPaths:@ [indexPath] withRowAnimation:UITableViewRowAnimationNone]; [tableView endUpdates]; }' – JohnChen

相關問題