异步下载图片,动态设置cell高度

在网上找到了一段不错的代码,笔记一下

  1. #pragma mark - UITableViewDelegate, UITableViewDataSource  
  2. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {  
  3.     return 1;  
  4. }  
  5.   
  6. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {  
  7.     return self.imgArray.count;//图片URL以数组的形式存在  
  8. }  
  9.   
  10. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {  
  11.     // 先从缓存中查找图片  
  12.     UIImage *image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKeyself.imgArray[indexPath.row]];  
  13.       
  14.     // 没有找到已下载的图片就使用默认的占位图,当然高度也是默认的高度了,除了高度不固定的文字部分。  
  15.     if (!image) {  
  16.         image = [UIImage imageNamed:kDownloadImageHolder];  
  17.     }  
  18.   
  19.     //手动计算cell  
  20.     CGFloat imgHeight = image.size.height * [UIScreen mainScreen].bounds.size.width / image.size.width;  
  21.     return imgHeight;  
  22. }  
  23.   
  24. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
  25.     static NSString *imgID = @"pictureCellID";  
  26.     PYClubPresentDetailImgTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:imgID];  
  27.     if (nil == cell) {  
  28.         cell = [[PYClubPresentDetailImgTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:imgID];  
  29.     }  
  30.     [self configureCell:cell atIndexPath:indexPath];  
  31.     cell.userInteractionEnabled = NO;  
  32.     return cell;  
  33. }  
  34.   
  35. - (void)configureCell:(PYClubPresentDetailImgTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {  
  36.     NSString *imgURL = self.imgArray[indexPath.row];  
  37.     UIImage *cachedImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:imgURL];  
  38.       
  39.     if ( !cachedImage ) {  
  40.         [self downloadImage:self.imgArray[indexPath.row] forIndexPath:indexPath];  
  41.         [cell.btn setBackgroundImage:[UIImage imageNamed:kDownloadImageHolder] forState:UIControlStateNormal];  
  42.     } else {  
  43.         [cell.btn setBackgroundImage:cachedImage forState:UIControlStateNormal];  
  44.     }  
  45. }  
  46.   
  47. - (void)downloadImage:(NSString *)imageURL forIndexPath:(NSIndexPath *)indexPath {  
  48.     // 利用 SDWebImage 框架提供的功能下载图片  
  49.     [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:imageURL] options:SDWebImageDownloaderUseNSURLCache progress:^(NSInteger receivedSize, NSInteger expectedSize) {  
  50.         // do nothing  
  51.     } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {  
  52.         [[SDImageCache sharedImageCache] storeImage:image forKey:imageURL toDisk:YES];  
  53.         dispatch_async(dispatch_get_main_queue(), ^{  
  54.             [self.tableView reloadData];  
  55.         });  
  56.     }];  
  57. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值