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


#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];
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM