iOS - UITableView加載網絡圖片 cell適應圖片高度


使用xib創建自定制cell   顯示圖片   創建一個繼承UITableViewCell的類   勾選xib

如下是xib創建圖

xib

 

向.h拖拽一個關聯線

.h

.m

 2.代碼創建(使用三方適配庫進行適配Masonry三方代碼適配)

.h

#import <UIKit/UIKit.h>

 

@interface NFTrailerNextTableViewCell : UITableViewCell

@property (nonatomic, strong) UIButton *imageBtn;

@end

 

.m

#import "NFTrailerNextTableViewCell.h"

 

@implementation NFTrailerNextTableViewCell

 

- (void)awakeFromNib {

    [super awakeFromNib];

    // Initialization code

}

 

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {

        

        [self.contentView addSubview:self.imageBtn];

        [self.imageBtn mas_makeConstraints:^(MASConstraintMaker *make) {

            make.left.offset(0);

            make.top.offset(0);

            make.right.offset(0);

            make.bottom.offset(-10);

        }];

    }

    return self;

}

 

- (UIButton *)imageBtn {

    if (nil == _imageBtn) {

        _imageBtn = [[UIButton alloc] init];

        _imageBtn.userInteractionEnabled = NO;

    }

    return _imageBtn;

}

 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];

 

    // Configure the view for the selected state

}

 

@end

 

 下面是公用的tableView的代理方法   以及使用SDImageView加載圖片的方法

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    

    //self.dataArr.count

    return self.imagearr.count;

    

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    

    NFTrailerNextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([NFTrailerNextTableViewCell class])];

    [self configureCell:cell atIndexPath:indexPath];

    cell.userInteractionEnabled = NO;

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;

}

 

//加載圖片

- (void)configureCell:(NFTrailerNextTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {

    NSString *imgURL = self.imagearr[indexPath.row];

    UIImage *cachedImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:imgURL];

    if ( !cachedImage ) {

        [self downloadImage:self.imagearr[indexPath.row] forIndexPath:indexPath];

        [cell.imageBtn setBackgroundImage:[UIImage imageNamed:@"國投互聯"] forState:UIControlStateNormal];

    } else {

        [cell.imageBtn 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, NSURL * _Nullable targetURL) {

        

    } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {

        [[SDImageCache sharedImageCache] storeImage:image forKey:imageURL toDisk:YES completion:^{

            

        }];

        dispatch_async(dispatch_get_main_queue(), ^{

            [self.NFTableView reloadData];

        });

    }];

}

 

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    // 先從緩存中查找圖片

    UIImage *image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:self.imagearr[indexPath.row]];

    // 沒有找到已下載的圖片就使用默認的占位圖,當然高度也是默認的高度了,除了高度不固定的文字部分。

    if (!image) {

        image = [UIImage imageNamed:@"國投互聯"];

    }

    //手動計算cell

    CGFloat imgHeight = image.size.height * [UIScreen mainScreen].bounds.size.width / image.size.width;

    return imgHeight;

}

 

這樣就可以了再見


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM