使用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;

}

这样就可以了再见

iOS - UITableView加载网络图片 cell适应图片高度的更多相关文章

  1. IOS UITableView 加载未知宽高图片的解决方案

    在开发中遇到了UITableView列表 UITableViewCell装载图片但不知Image的宽高 问题. 在解决该问题的时候,首先想到的是异步加载图片 采用第三方框架SDWebImage 实现对 ...

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

    #pragma mark- UITableView - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSI ...

  3. Android笔记之使用Glide加载网络图片、下载图片

    Glide简介 不想说太多,真的很方便:P)可以节省我不少时间 GitHub地址:https://github.com/bumptech/glide 加载网络图片到ImageView Glide.wi ...

  4. IOS延时加载网络图片

        重网上下载图片是很慢的,为了不影响体验,选择延时加载图片是很好的办法. 一个tableView 列表,左边暂时没有图 - (UITableViewCell *)tableView:(UITab ...

  5. IOS UIwebView 加载网络图片 使用相对地址

    方法一: 在html文件内直接使用file:///user//xx//image.png的绝对路径 注:这样可以显示图片,但是如果在程序目录修改,图片就不能显示 方法二: 在html使用占位符,如:在 ...

  6. (BUG已修改,最优化)安卓ListView异步加载网络图片与缓存软引用图片,线程池,只加载当前屏之说明

    原文:http://blog.csdn.net/java_jh/article/details/20068915 迟点出更新的.这个还有BUG.因为软引应不给力了.2.3之后 前几天的原文有一个线程管 ...

  7. iOS WebView 加载本地资源(图片,文件等)

    https://www.cnblogs.com/dhui69/p/5596917.html iOS WebView 加载本地资源(图片,文件等) NSString *path = [[NSBundle ...

  8. 【iOS入门】UITableView加载图片

    学习带图片的列表 官方 LazyTableImages demo  http://download.csdn.net/detail/jlyidianyuan/5726749 分析源码是学习的好方法. ...

  9. iOS网络加载图片缓存与SDWebImage

    加载网络图片可以说是网络应用中必备的.如果单纯的去下载图片,而不去做多线程.缓存等技术去优化,加载图片时的效果与用户体验就会很差. 一.自己实现加载图片的方法 tips: *iOS中所有网络访问都是异 ...

随机推荐

  1. NoSQL 非关系数据库

    NoSQL 数据库的学习 Redis的Windows版本安装 待整理 redis 安装 关于分布式的网站介绍 NOSQL 几个网页 认识MongoDB Mongodb实现副本集和Mongodb副本集的 ...

  2. IPython介绍及安装

    IPython介绍 - CSDN博客https://blog.csdn.net/gavin_john/article/details/53086766 python命令行在windows下实现tab自 ...

  3. STM8在IAR中Option Byte配置

    转http://www.stmcu.org/module/forum/thread-607140-1-1.html 近期帮客户调试STM8的程序使用到OptionByte配置,在网上查询几乎都是介绍如 ...

  4. SpringBoot自定义序列化的使用方式--WebMvcConfigurationSupport

    场景及需求: 项目接入了SpringBoot开发,现在需求是服务端接口返回的字段如果为空,那么自动转为空字符串. 例如: [     {         "id": 1,      ...

  5. svn up 排除目录更新

    svn update --set-depth=exclude tmp 则可以排除tmp目录的更新

  6. Jenkins管理静态资源

    这里我们的前端是使用webpack来管理静态资源的,把静态资源上传到svn上面来管理 这里我们把项目和静态资源剥离开来,然后静态资源接入CDN 我们的svn的结构是这样的 我们需要把这些目录都进行打包 ...

  7. Mac 配置 Jenkins

    关于 Jenkins Jenkins 是一个开源软件项目,旨在提供一个开放易用的软件平台,使持续集成变成可能. 安装 Jenkins 并配置, http://linjunpop.logdown.com ...

  8. jsp 嵌入页面

    <section id="main-content"> <section class="wrapper"> <jsp:includ ...

  9. opencv之haar特征+AdaBoos分类器算法流程(三)

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...

  10. ubuntu18.04下安装Anaconda及numpy、matplotlib

    为了学习深度学习,我需要首先掌握利用python进行科学计算的知识,顺便复习一下线性代数.微积分.概率论.当然,现在我要做的是安装Anaconda. 1.官网下载,linux版本:https://ww ...