1.創(chuàng)建一個裝高度的數(shù)組并初始化
@property(nonatomic,strong)NSMutableArray*imagesHeightArray;
2.在獲取數(shù)據(jù)的時候,給數(shù)組添加賦值0
[self.imagesHeightArray addObject:@(0)];
3.在cell里面,取圖片的時候,取每一張圖片的尺寸,并使用block傳值,把高度傳給數(shù)組
定義block:
@property (nonatomic, copy) void(^ImgDetailTCblock)(ImgDetailTC*cell,CGFloat height);
@property (nonatomic, strong) NSIndexPath* indexPath;
傳值:
[self.imgimageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", goods.detail]]
completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
// NSLog(@"寬:%f, 高:%f", image.size.width, image.size.height);
CGFloat imgHeight=image.size.height * [UIScreen mainScreen].bounds.size.width / image.size.width;
self.ImgDetailTCblock(wself, imgHeight);
}];
4.在viewController.m里
給數(shù)組傳值
cell.indexPath=indexPath;
cell.goods = goods;
__weak __typeof(&*self) wself = self;
[cell setImgDetailTCblock:^(ImgDetailTC*cell,CGFloat height) {
CGFloat oldHeight=[[self.imagesHeightArray objectAtIndex:cell.indexPath.row] doubleValue];
if(oldHeight == height){
return ;
}
[wself.imagesHeightArray replaceObjectAtIndex:cell.indexPath.row withObject:@(height)];
[wself.tableView reloadData];
}];
每個cell的高度
CGFloat height = [[self.imagesHeightArray objectAtIndex:indexPath.row] doubleValue];
return height;