使用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. mysql 查询 根据时分秒取数据 比如 取 时间为 8点半的 dateformat 时间函数转换

     date_format(date,'%H') = 8 and date_format(date,'%i') = 30   SELECT * FROM `t_pda_trucklog` WHERE D ...

  2. 如何取消或定制当点击GridView 的时候出现的那个黄色背景

    初始化的时候在代码里面设置setSelector颜色为透明色 gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));

  3. 【转载】Chrome插件开发 尝试

    本来来自 http://www.cnblogs.com/rufus-hua/ 1.新建文件夹 如图:整个项目的结构 2.新建一个名为manifest.json的文件,编码模式为utf-8,(可以先建好 ...

  4. 还没被玩坏的robobrowser(3)——简单的spider

    背景 做一个简单的spider用来获取python selenium实战教程的一些基本信息.因为python selenium每年滚动开课,所以做这样一个爬虫随时更新最新的开课信息是很有必要的. 预备 ...

  5. Shell脚本开发规范

    一.前言 由于工作需要,最近重新开始拾掇shell脚本.虽然绝大部分命令自己平时也经常使用,但是在写成脚本的时候总觉得写的很难看.而且当我在看其他人写的脚本的时候,总觉得难以阅读.毕竟shell脚本这 ...

  6. winform中键盘和鼠标事件的捕捉和重写

    在编写winform应用程序时,有时需要无论在哪个控件获取焦点时,对某一个键盘输入或者鼠标事件都进行同样的操作.比如编写一个处理图片的应用程序时,希望无论当前哪个控件获得焦点,当用户按上.下.左.右键 ...

  7. stm8 stvd工程头文件包含方式

    方式一:在Include  Files上右键 add files to folder把.h文件添加到该组下, 添加完编译成功之后,下面窗口会多出一个预处理包含路径(若有错误没有编译成功,则看不到) 方 ...

  8. MySQL (1366, "Incorrect string value: '\\xF0\\x9F\\x8E\\xAC\\xE5\\x89...' for column 'description' at row 1")

    (1366, "Incorrect string value: '\\xF0\\x9F\\x8E\\xAC\\xE5\\x89...' for column 'description' at ...

  9. C#基础第九天-作业答案-储蓄账户(SavingAccount)和信用账户(CreditAccount)

    class Bank { //Dictionary<long,Account> dictionary=new Dictionary<long,Account>(); DataT ...

  10. IReport5.6.0创建数据库连接找不到驱动(iReport中ClassNotFoundError错误的解决)

    情景:iRoport中选择com.microsoft.jdbc.sqlserver.SQLServerDriver的JDBC Driver;连接时出现ClassNotFoundError错误 当见到下 ...