iOS之UITableView加载网络图片cell自适应高度

本文介绍了一个使用UITableView展示图片的iOS应用案例,包括图片的缓存与加载机制。通过SDWebImage实现了图片的异步下载及缓存,并在UITableView中高效地显示了图片。此外,还介绍了如何自定义UITableViewCell来适配图片展示。
#pragma mark-  UITableView
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    UIImage *img = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:_productDetailImageAry[indexPath.row]];
    if (!img) {
        img =  [UIImage imageNamed:@"产品默认图"];
    }
    CGFloat height = img.size.height;
    return (height/img.size.width)*CScreenWidth;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _productDetailImageAry.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MDSListImageCell   *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [self confirmCell:cell atIndexPath:indexPath];
    return cell;
}
- (void)confirmCell:(MDSListImageCell *)cell atIndexPath:(NSIndexPath *)indexPath{
    NSString *imgUrl = _productDetailImageAry[indexPath.row];
    UIImage *cachedImg = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:imgUrl];
    if (!cachedImg) {
        [self downloadImage:imgUrl forIndexPath:indexPath];
    }else{
        cell.imageShow  =cachedImg;
    }
}
- (void)downloadImage:(NSString *)imageURL forIndexPath:(NSIndexPath *)indexPath{
    __weak typeof(self) weakSelf = self;
    [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:SDIMAGE_URL(imageURL)
                                                          options:2
                                                         progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                                                         }
                                                        completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
                                                            [[SDImageCache sharedImageCache] storeImage:image forKey:imageURL toDisk:YES];
                                                            [weakSelf performSelectorOnMainThread:@selector(reloadCellAtIndexPath:) withObject:indexPath waitUntilDone:NO];
                                                            
                                                        }];
}
-(void)reloadCellAtIndexPath:(NSIndexPath *)indexPath {
    [_listTableView reloadData];
}

 

转载于:https://www.cnblogs.com/xianfeng-zhang/p/7942471.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值