UICollectionView之网络图片解析
1:将SDWebImage文件夹的类库导入工程,创建一个模型对象Model类,并声明好它的属性,再创建一个继承自UICollectionViewCell的自定义类
2:在自定义cell类中重写
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.imageView = [[UIImageView alloc] init];
self.imageView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
[self.contentView addSubview:_imageView];
_imageView.backgroundColor = [UIColor blueColor];
}
return self;
}
3:创建一个增广视图的类继承自 UICollectionReusableView,然后将需要显示的控件声明成属性,重写初始化方法
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.headerLabel = [[UILabel alloc] initWithFrame:frame];
self.headerLabel.textAlignment = NSTextAlignmentCenter;
self.headerLabel.font = [UIFont systemFontOfSize:40];
[self addSubview:_headerLabel];
}
return self;
}
4:在viewController的.m文件中开始编写代码
1):创建collectionView的注意事项:
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
然后通过layout设置相关属性
UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:layout];
collectionView.dataSource = self;
//cell要显示内容的话必须注册显示的控件类
[collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
[collectionView registerClass:[MyCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
2):解析数据
- (void)handleData {
NSString *filePath = [ [NSBundle mainBundle] pathForSource:@"Data" ofType:@"json"];
NSData *data = [NSData dataWithContentOfFilePath:filePath];
NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil ] ;
_dataArray = [[NSMutableArray alloc] initWithCapacity:0];
for (NSDictionary *dic in array) {
Model *model = [[Model alloc] init];
[model setValuesForKeysWithDictionary:dic];
[_dataArray addObject:model];
}
}
3):实现datasource的代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [_dataArray count];
}
4):实现两个非常重要的方法
a:cell的使用的方法
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
Model *model = _dataArray[indexPath.row];
NSURL *url = [NSURL URLWithString:model.thumbURL];
[cell.imageView sd_setImageWithURL:url placeholderImaeg:[UIImage imageNamed:@"3.png"]];
return cell;
}
b:增广视图使用的方法
- (UICollectionReusableView *)
collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
MyCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];//此处的类是MyCollectionReusableView
view.headerLabel.text = @"网络图片";
return view;
}
return nil;
转载于:https://www.cnblogs.com/arenouba/p/5217270.html
UICollectionView之网络图片解析相关推荐
- android 图片占用内存大小及加载解析
*本篇文章已授权微信公众号 guolin_blog (郭霖)独家发布 在讲解图片占用内存前,我们先问自己几个问题: 我们在对手机进行屏幕适时,常想可不可以只切一套图适配所有的手机呢? 一张图片加载到手 ...
- Android VR Player(全景视频播放器) [7]:视频列表的实现-网络视频
Android VR Player(全景视频播放器) [7]:视频列表的实现-网络视频 前期准备 在之前的博文,Android VR Player(全景视频播放器) [6]:视频列表的实现-本地视频 ...
- 基于百度AI的文字识别(Python语言)
简 介:百度大脑是百度 AI 核心技术引擎,包括视觉.语音.自然语言处理.知识图谱.深度学习等AI核心技术和AI开放平台.本文介绍百度 AI 核心技术中文字识别功能的使用方法. 关键词:百度AI.文字 ...
- Android Volley完全解析2:使用Volley加载网络图片
原文链接:http://blog.csdn.net/guolin_blog/article/details/17482165,CSDN 郭霖 在上一篇文章中,我们了解了Volley到底是什么,以及它的 ...
- Google官方网络框架-Volley的使用解析Json以及加载网络图片方法
Google官方网络框架-Volley的使用解析Json以及加载网络图片方法 Volley是什么?Google I/O 大会上,Google 推出 Volley的一个网络框架Volley适合什么场景? ...
- Android Volley完全解析(二),使用Volley加载网络图片 转载:http://blog.csdn.net/guolin_blog/article/details/174
转载:http://blog.csdn.net/guolin_blog/article/details/17482165 在上一篇文章中,我们了解了Volley到底是什么,以及它的基本用法.本篇文章中 ...
- android加载富文本可点击链接,Android富文本解析器,支持网络图片,图片和链接点击事件...
HtmlText image HtmlText 是 android.text.Html 的一个扩展,可以加载 HTML 并将其转换成 Spannable 显示在 TextView 上,支持网络图片,图 ...
- AndroidStudio_安卓原生开发_请求网络图片并解析成BitMap_异步处理_在UI线程执行_利用AsyncTask---Android原生开发工作笔记146
直接看代码吧. //1.这里获取图片runOnUiThread(new Runnable() {@Overridepublic void run() {SharedPreferences settin ...
- VVeboTableView 源码解析
原文链接:http://www.jianshu.com/p/78027a3a2c41 最近在看一些 iOS 性能优化的文章,我找到了 VVeboTableView 这个框架.严格来说这个不属于框架,而 ...
最新文章
- ELK(+Redis)+LogAnalyzer解决企业日志问题
- plotly可视化绘制双子图(subplots)
- 程序控制发送文件到邮箱_Intouch邮件发送的真实案例(一),不可多得的技术尝试...
- 微博面试Java,微博java开发工程师面试题整理
- (诊断)处理错误fatal error: Python.h: No such file or directory
- 无监督分类:聚类分析(K均值)
- 归属地的判断规则有吗_IPO|创业板注册制规则-详解股权激励新规
- 开源一周岁,MindSpore新特性巨量来袭
- 转] 两种自定义表单设计方案
- 行业发展 | 雷达信号处理领域面临的重大问题
- Poco库学习——1
- 新《葫芦兄弟》被批毁童年,如果这样拍必然好看一百倍!
- 水杯如何测试 (测试用例)
- 解决:向日葵连接已断开
- 推拿师考证需要什么条件
- iOS开发——Siri语音识别
- 每天学点说话技巧+人性的弱点 +陈吉宁
- CnOpenData公共数据专区上新 | 中文金融情感词典
- python抖音github_利用python 下载抖音上流行音乐
- Unity制作 小球吃金币 游戏
热门文章
- python期末考试及答案广东卷_python数据分析答案试题题目及答案,期末考试题库,章节测验答案...
- mysql err 1349_MySQL 视图 第1349号错误解决方法
- 删除第一个_可能是圆谷最后悔的决定!他本应是中国第一个奥特曼,惨遭删除...
- python做定时任务的方式及优缺点_python BlockingScheduler定时任务及其他方式的实现...
- c语言表达式3178的值为,【C语言】C语言运算符
- linux共享存储通信实验,Linux进程通信——共享存储
- Python OpenCV分水岭算法分割和提取重叠或有衔接的图像中的对象
- 2021 年第十一届 MathorCup 高校数学建模挑战赛A题分析
- std::max,std::min错误:应输入标识符的解决方法
- vdi voi idv区别_VDI,IDV,VOI究竟有何不同