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
-(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适应图片高度的更多相关文章
- IOS UITableView 加载未知宽高图片的解决方案
在开发中遇到了UITableView列表 UITableViewCell装载图片但不知Image的宽高 问题. 在解决该问题的时候,首先想到的是异步加载图片 采用第三方框架SDWebImage 实现对 ...
- iOS之UITableView加载网络图片cell自适应高度
#pragma mark- UITableView - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSI ...
- Android笔记之使用Glide加载网络图片、下载图片
Glide简介 不想说太多,真的很方便:P)可以节省我不少时间 GitHub地址:https://github.com/bumptech/glide 加载网络图片到ImageView Glide.wi ...
- IOS延时加载网络图片
重网上下载图片是很慢的,为了不影响体验,选择延时加载图片是很好的办法. 一个tableView 列表,左边暂时没有图 - (UITableViewCell *)tableView:(UITab ...
- IOS UIwebView 加载网络图片 使用相对地址
方法一: 在html文件内直接使用file:///user//xx//image.png的绝对路径 注:这样可以显示图片,但是如果在程序目录修改,图片就不能显示 方法二: 在html使用占位符,如:在 ...
- (BUG已修改,最优化)安卓ListView异步加载网络图片与缓存软引用图片,线程池,只加载当前屏之说明
原文:http://blog.csdn.net/java_jh/article/details/20068915 迟点出更新的.这个还有BUG.因为软引应不给力了.2.3之后 前几天的原文有一个线程管 ...
- iOS WebView 加载本地资源(图片,文件等)
https://www.cnblogs.com/dhui69/p/5596917.html iOS WebView 加载本地资源(图片,文件等) NSString *path = [[NSBundle ...
- 【iOS入门】UITableView加载图片
学习带图片的列表 官方 LazyTableImages demo http://download.csdn.net/detail/jlyidianyuan/5726749 分析源码是学习的好方法. ...
- iOS网络加载图片缓存与SDWebImage
加载网络图片可以说是网络应用中必备的.如果单纯的去下载图片,而不去做多线程.缓存等技术去优化,加载图片时的效果与用户体验就会很差. 一.自己实现加载图片的方法 tips: *iOS中所有网络访问都是异 ...
随机推荐
- golang项目结构
一.概述 golang更新了环境变量,导致整个goproject的结构改变: [root@node175 goproject]# tree -L . ├── bin │ └── demo ├── ...
- 测试Js权限
'12222' 测试一下 刚兴趣的可以参考: http://www.cnblogs.com/littledu/archive/2011/05/08/2040298.html http://www.cn ...
- Oracle 12C -- 扩展varchar2、nvarchar2、和raw数据类型的大小限制
在12C中,varchar2,nvarchar2和raw类型从之前的4K扩展到32K 升级到12C后,参数max_string_size默认值是standard,即不改变varchar2.nvarch ...
- MySQL-慢查询日志
慢查询日志功能默认不开启,其记录了执行时间超过参数long_query_time的值(默认是10),且访问的行数超过了参数min_examined_row_limit的值得SQL语句. mysql&g ...
- sql用逗号连接多张表对应哪个join?
转自:http://blog.csdn.net/huanghanqian/article/details/52847835 四种join的区别已老生常谈: INNER JOIN(也可简写为JOIN): ...
- 两张图看清SharePoint 2013 Farm 逻辑体系结构
前篇文章分析了SharePoint 2013 的物理拓扑结构.物理拓扑分为3层(2层),详情参见<SharePoint 2013 Farm (多层服务器)安装指南——Least Privileg ...
- netty服务端实现心跳超时的主动拆链
一.服务器启动示例: public class MySocketServer { protected static Logger logger = LoggerFactory.getLogger(My ...
- Ubuntu 添加安装字体
Ubuntu的美化当然少不了漂亮字体的支持,我有时会code一下,所以喜欢adobe的 source code pro(开源),安装步骤如下: (注:如果想导入的字体是一个单ttf文件的,可以直接双击 ...
- Windows 上 怎么安装 install elasticsearch plugin
D:\elasticsearch-5.2.1\bin>elasticsearch-plugin install file:///D:/elasticsearch -5.2.1/bin/elast ...
- UITableView/UIScrollView 不能响应TouchBegin 的处理 及窥见 hitTest:withEvent:
重写touchBegin 方法是不行的,在UITableView/UIScrollView 解决方案 重写hitTest:withEvent: 在他们的子类中 - (UIView *)hitTest ...