IOS cell自适应网络图片高度

1.创建一个装高度的数组并初始化

    @property(nonatomic,strong)NSMutableArray*imagesHeightArray;

2.在获取数据的时候,给数组添加赋值0
[self.imagesHeightArray addObject:@(0)];
3.在cell里面,取图片的时候,取每一张图片的尺寸,并使用block传值,把高度传给数组

定义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里

给数组传值

            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;

你可能感兴趣的:(IOS cell自适应网络图片高度)