2017-03-08 1 views
1

이미지를 다운로드 한 후 이미지 뷰의 높이를 조정하고 싶지만 처음 테이블 뷰를로드 할 때 첫 번째 셀 이미지가 잘못되었습니다. i는 셀을 다시 표시 될 수 있도록 테이블을 이동하면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의 콘텐츠 모드를 가로 세로로 설정할 수 있습니다. 이 타이밍

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

관련 문제