使用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. windows下安装phpredis模块 (转)

    1.下载: http://pecl.php.net/package/redis/2.2.7/windows 2.下载后 由于里面有两个模块分别是vc6,vc9编译的,我们需要知道我们的Php是vc6还 ...

  2. PHP中数字检测is_numeric与ctype_digit的区别介绍

    PHP中的两个函数is_numeric和ctype_digit都是检测字符串是否是数字,但也存在一点区别 is_numeric:检测是否为数字字符串,可为负数和小数 ctype_digit:检测字符串 ...

  3. docker学习笔记 --- centos install

    Docker简介: Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源. Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后发 ...

  4. MongoDB学习笔记(10)-- 排序

    MongoDB sort() 方法 在 MongoDB 中使用 sort() 方法对数据进行排序,sort() 方法可以通过参数指定排序的字段,并使用 1 和 -1 来指定排序的方式,其中 1 为升序 ...

  5. 【蓝桥杯】PREV-21 回文数字

    题目链接:http://lx.lanqiao.org/problem.page? gpid=T113   历届试题 回文数字   时间限制:1.0s   内存限制:256.0MB      问题描写叙 ...

  6. matlab入门笔记(二):矩阵和数组

    摘自<matlab从入门到精通>胡晓东 matlab最基本的数据结构就是矩阵,一个二维的.长方形形状的数据,可以用易于使用的矩阵形式来存储,这些数据可以是数字,字符.逻辑状态,甚至是mat ...

  7. DVWA安装——一个菜鸟的入门教程

    DVWA的安装非常简单: 1.更改config/config.inc.php文件中的数据库配置信息 2.访问setup.php,点击create/reset database即可 3.默认用户名/密码 ...

  8. 一种3D空间的柱状多边形检测实现

    最近无意中拓展出这个东西,基于之前写的2D多边形检测: http://www.cnblogs.com/hont/p/6105997.html 而判断两条线相交的方法替换成了我后来写的差乘判断: htt ...

  9. 5. MIZ7035 PCIe测试 RIFFA【PCIE视频传输】

    1.前言 MIZ7035官方提供了两种pcie的demo,一个就是普通的PIO测试,一个是BMD测试.我只是试验了PIO功能,可以对板卡直接进行IO寄存器读写.而另外一个BMD功能使用了DMA来加速数 ...

  10. Xilinx FPGA用户约束文件(转自xilinx ISE 开发指南

    FPGA设计中的约束文件有3类:用户设计文件(.UCF文件).网表约束文件(.NCF文件)以及物理约束文件(.PCF文件),可以完成时序约束.管 脚约束以及区域约束.3类约束文件的关系为:用户在设计输 ...