项目中,有一个需求,是加载几张网络图片,每一张都要求宽度与屏幕宽度一样,然后高度自适应;并且几张自上而下一次排列。

这让我想到了UITableVIew,但是图片的处理成了问题,经过我多次的处理,完成了这个需求

大概的思路是:

1.重写自己的Cell,Cell中利用一个UIButton展示图片,而不是UIImageView,因为这样好控制图片的宽度与屏幕一致

2.每一个图片在未完成网络加载时,都有一张长款固定的HolderImage代替

3.UITableView cell 的height手动计算

首先是自定义Cell:

//
//  PYClubPresentDetailImgTableViewCell.h
//  iphone-pay
//
//  Created by jinhui005 on 16/8/25.
//  Copyright © 2016年 RHJX Inc. All rights reserved.
//#import <UIKit/UIKit.h>@interface PYClubPresentDetailImgTableViewCell : UITableViewCell@property (nonatomic, strong) UIButton *btn;@end
//
//  PYClubPresentDetailImgTableViewCell.m
//  iphone-pay
//
//  Created by jinhui005 on 16/8/25.
//  Copyright © 2016年 RHJX Inc. All rights reserved.
//#import "PYClubPresentDetailImgTableViewCell.h"@interface PYClubPresentDetailImgTableViewCell ()@end@implementation PYClubPresentDetailImgTableViewCell- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];if (self) {[self.contentView addSubview:self.btn];[self.btn mas_makeConstraints:^(MASConstraintMaker *make) {make.left.top.right.bottom.equalTo(self.contentView);}];}return self;
}- (UIButton *)btn {if (nil == _btn) {_btn = [[UIButton alloc] init];_btn.titleLabel.font = kFT3;_btn.userInteractionEnabled = NO;}return _btn;
}@end

然后是UITableView的代理方法

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {return 1;
}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return self.imgArray.count;//图片URL以数组的形式存在
}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {// 先从缓存中查找图片UIImage *image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey: self.imgArray[indexPath.row]];// 没有找到已下载的图片就使用默认的占位图,当然高度也是默认的高度了,除了高度不固定的文字部分。if (!image) {image = [UIImage imageNamed:kDownloadImageHolder];}//手动计算cellCGFloat imgHeight = image.size.height * [UIScreen mainScreen].bounds.size.width / image.size.width;return imgHeight;
}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {static NSString *imgID = @"pictureCellID";PYClubPresentDetailImgTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:imgID];if (nil == cell) {cell = [[PYClubPresentDetailImgTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:imgID];}[self configureCell:cell atIndexPath:indexPath];cell.userInteractionEnabled = NO;return cell;
}- (void)configureCell:(PYClubPresentDetailImgTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {NSString *imgURL = self.imgArray[indexPath.row];UIImage *cachedImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:imgURL];if ( !cachedImage ) {[self downloadImage:self.imgArray[indexPath.row] forIndexPath:indexPath];[cell.btn setBackgroundImage:[UIImage imageNamed:kDownloadImageHolder] forState:UIControlStateNormal];} else {[cell.btn 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) {// do nothing} completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {[[SDImageCache sharedImageCache] storeImage:image forKey:imageURL toDisk:YES];dispatch_async(dispatch_get_main_queue(), ^{[self.tableView reloadData];});}];
}

iOS - UITableView加载网络图片 cell适应图片高度相关推荐

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

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

  2. iOS中加载Flutter中的图片

    在 Flutter 插件开发中,有时需要将 Flutter 中配置的图片资源传递到 Android 或者是 iOS原生中,传递方法如下: //一般应用在Flutter 插件开发中 //注册插件的方法 ...

  3. iOS UILabel加载html点击图片查看大图 附demo

    我们在有些时候,因为性能和加载时间的问题,需要用UILabel加载html的方式来代替webview. 大部分情况,UILabel都可以很好的展示出想要的效果,但是却不能满足点击查看大图的需求. 本解 ...

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

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

  5. iOS 动态加载LaunchScreen上的图片

    前言 hihi,勇敢的小伙伴儿们大家好,明天就要放元旦假期了,小长假三天你们准备去哪玩呢? 我就不出去玩了,买了一本新书在家攻读,如果有时间有机会的话我会写成博客跟大家一起分享. 今天分享的这个吧,恕 ...

  6. html多个图片成一个图,ios webview 加载HTML 多个图片URL 拼接成一张图片

    有head的HTML 1.拼接HTML 2.创建webview后  用这个方法加载HTML [self.webView loadHTMLString:[self reSizeImageWithHTML ...

  7. flutter加载网络图片与本地图片

    >最后附带整体源码: 1.网络请求图片效果展示:附带容器整体设计 实现代码: 里面的网络图片请求地址可用 /*** 显示网络图片*/ class WebRequest extends State ...

  8. Flutter 中的 Image 组件 如何加载网络图片和本地图片

    如下,直接上代码说明: mian(){ runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build( ...

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

    NSString *path = [[NSBundle mainBundle] pathForResource:@"关于.docx" ofType:nil]; NSURL *url ...

最新文章

  1. Java 深、浅克隆
  2. NR 5G 世界通信发展史简述
  3. 计算机 维修 pdf,简单计算机维修..pdf
  4. s6-5 TCP 连接的建立
  5. 前端开源项目周报0307
  6. 今天的虎兄才跳了过去的飞秋WZ132
  7. iOS开发使用UIKeyInput自定义密码输入框
  8. wiki——Isogeometric_analysis
  9. Maven ,命令行中,字符编码 设置
  10. window 服务器443端口占用,443和80端口被占用win10如何解决_443和80端口被占用win10怎么处理...
  11. 开放PLM——Aras Innovator编程学习(二)AML脚本组成
  12. Error opening zip file or JAR manifest missing
  13. 自学编程,十年磨一剑
  14. 编译工具make、gmake、cmake、nmake和Dmake的区别
  15. Win10下Python3.x环境安装和配置教程
  16. 终点条件下的轨迹预测:It is not the Journey but the Destination: Endpoint Conditioned Trajectory Prediction
  17. 卸载微软拼音2003
  18. GuestOS? HostOS?
  19. 计算机符号系统c,附录c符号计算系统mathematica的.doc
  20. Spring 学习笔记----->AOP

热门文章

  1. scrapy爬取旋风网站APP
  2. Android性能优化之线程池策略和对线程池的了解
  3. swiftUI关于背景色
  4. VUE项目打包(解决背景图片不显示问题)
  5. HDU--4248、A Famous Stone Collector (计数类dp)
  6. 千牛包表包下载_探索R中表包的gt语法
  7. 【百度飞桨Paddle】11类食品分类项目
  8. 患有散光怎么办?这2个办法,或许能帮到你!
  9. JabRef文献管理软件简明教程
  10. python读取txt文件并求和,Python读取txt文件数据的方法(用于接口自动化参数化数据)...