iOS - UITableView加载网络图片 cell适应图片高度【转】

项目中,有一个需求,是加载几张网络图片,每一张都要求宽度与屏幕宽度一样,然后高度自适应;并且几张自上而下一次排列。
这让我想到了UITableVIew,但是图片的处理成了问题,经过我多次的处理,完成了这个需求
大概的思路是:
1.重写自己的Cell,Cell中利用一个UIButton展示图片,而不是UIImageView,因为这样好控制图片的宽度与屏幕一致
2.每一个图片在未完成网络加载时,都有一张长款固定的HolderImage代替
3.UITableView cell 的height手动计算

首先是自定义Cell:

  
#import <UIKit/UIKit.h>  
  
@interface PYClubPresentDetailImgTableViewCell : UITableViewCell  
  
@property (nonatomic, strong) UIButton *btn;  
  
@end  
#import "PYClubPresentDetailImgTableViewCell.h"  
  
@interface PYClubPresentDetailImgTableViewCell ()  
  
  
@end  
  
@implementation PYClubPresentDetailImgTableViewCell  
  
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {  
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];  
    if (self) {  
  
        [self.contentView addSubview:self.btn];  
        [self.btn mas_makeConstraints:^(MASConstraintMaker *make) {  
            make.left.top.right.bottom.equalTo(self.contentView);  
        }];  
    }  
    return self;  
}  
  
- (UIButton *)btn {  
    if (nil == _btn) {  
        _btn = [[UIButton alloc] init];  
        _btn.titleLabel.font = kFT3;  
        _btn.userInteractionEnabled = NO;  
    }  
    return _btn;  
}  
  
@end  

然后是UITableView的代理方法


#pragma mark - UITableViewDelegate, UITableViewDataSource  
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {  
    return 1;  
}  
  
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {  
    return self.imgArray.count;//图片URL以数组的形式存在  
}  
  
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {  
    // 先从缓存中查找图片  
    UIImage *image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey: self.imgArray[indexPath.row]];  
      
    // 没有找到已下载的图片就使用默认的占位图,当然高度也是默认的高度了,除了高度不固定的文字部分。  
    if (!image) {  
        image = [UIImage imageNamed:kDownloadImageHolder];  
    }  
  
    //手动计算cell  
    CGFloat imgHeight = image.size.height * [UIScreen mainScreen].bounds.size.width / image.size.width;  
    return imgHeight;  
}  
  
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
    static NSString *imgID = @"pictureCellID";  
    PYClubPresentDetailImgTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:imgID];  
    if (nil == cell) {  
        cell = [[PYClubPresentDetailImgTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:imgID];  
    }  
    [self configureCell:cell atIndexPath:indexPath];  
    cell.userInteractionEnabled = NO;  
    return cell;  
}  
  
- (void)configureCell:(PYClubPresentDetailImgTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {  
    NSString *imgURL = self.imgArray[indexPath.row];  
    UIImage *cachedImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:imgURL];  
      
    if ( !cachedImage ) {  
        [self downloadImage:self.imgArray[indexPath.row] forIndexPath:indexPath];  
        [cell.btn setBackgroundImage:[UIImage imageNamed:kDownloadImageHolder] forState:UIControlStateNormal];  
    } else {  
        [cell.btn setBackgroundImage:cachedImage forState:UIControlStateNormal];  
    }  
}  
  
- (void)downloadImage:(NSString *)imageURL forIndexPath:(NSIndexPath *)indexPath {  
    // 利用 SDWebImage 框架提供的功能下载图片  
    [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:imageURL] options:SDWebImageDownloaderUseNSURLCache progress:^(NSInteger receivedSize, NSInteger expectedSize) {  
        // do nothing  
    } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {  
        [[SDImageCache sharedImageCache] storeImage:image forKey:imageURL toDisk:YES];  
        dispatch_async(dispatch_get_main_queue(), ^{  
            [self.tableView reloadData];  
        });  
    }];  
}  

转载自>http://blog.csdn.net/icefishlily/article/details/52606221

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 项目中,有一个需求,是加载几张网络图片,每一张都要求宽度与屏幕宽度一样,然后高度自适应;并且几张自上而下一次排列。...
    i马阅读 1,677评论 2 1
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,259评论 4 61
  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,093评论 3 38
  • 李老头的电动车立了大功了。 电动车三个轮,后面一个小拖斗。用来运一点儿农产品很方便,还可以接接放学的孙女,还能接接...
    王新屏阅读 313评论 0 0
  • 补课两个星期之后,突然接到学校通知:继续放假直到一个星期后正式开学。可是我却舍不得离开,以前总是盼望着回家,...
    合秣阅读 574评论 0 0