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

来源:互联网 发布:mac开机进入客人用户 编辑:程序博客网 时间:2024/05/22 18:23

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

  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
原创粉丝点击