使用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. dubbo 学习(5) dubbo多协议和多注册中心

    转载 http://blog.csdn.net/songjinbin/article/details/49498431 一.配置dubbo多协议模式 1.默认协议 Dubbo缺省协议采用单一长连接和N ...

  2. spring 下载

    Spring官网(https://spring.io/)改版后,直接下载Jar包的链接,下面汇总 1.直接输入地址,改相应版本即可:http://repo.springsource.org/libs- ...

  3. [转]Intellij idea创建javaWeb以及Servlet简单实现

    准备:1. 安装jdk1.72. 安装tomcat1.8 一.创建并设置javaweb工程 1.创建javaweb工程File --> New --> Project... 设置工程名字: ...

  4. how-to-convert-string-to-localdate

    Few Java examples show you how to convert a String to the new Java 8 Date API – java.time.LocalDate ...

  5. 未能加载文件或程序集“Microsoft.SqlServer.Management.Sdk.Sfc, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91”或它的某一个依赖项。系统找不到指定的文件。

    莫名其妙的,在 VS 中添加数据库连接就报这个错误,经过查找,解决方法是重新安装下两个sql server的组件:SharedManagementObjects.msi 和 SQLSysClrType ...

  6. iPhone-获取网络数据或者路径的文件名

    Phone中,在网络中的数据流中提取链接中的文件名称时,有很多方法,这里总结一些. 方法一:最直接. 1     NSString * urlString = @"http://www.ba ...

  7. 深入理解Linux内核-进程

    1.进程的静态特性 进程:程序执行时的一个实例 进程描述符(task_struct): 进程的基本信息(thread_info).指向内存区描述符的指针(mm_struct).进程相关的tty(tty ...

  8. tmux使用及配置

    这篇博客跟以前的vim配置一样,都是为了以后本人自己方便备份的tmux配置文件 0.安装 centos默认自带的软件是screen,没有带tmux,所以需要源代码安装 get clone http:/ ...

  9. 菜鸟学Java(十四)——Java反射机制(一)

    说到反射,相信有过编程经验的人都不会陌生.反射机制让Java变得更加的灵活.反射机制在Java的众多特性中是非常重要的一个.下面就让我们一点一点了解它是怎么一回事. 什么是反射 在运行状态中,对于任意 ...

  10. Backpropagation In Convolutional Neural Networks

    http://www.jefkine.com/general/2016/09/05/backpropagation-in-convolutional-neural-networks/ http://w ...