UICollectionView请求网络数据显示(Text)
TEXT:
AppDelegate.m
self.window.rootViewController = [[[UINavigationController alloc]initWithRootViewController:[RootViewController new]]autorelease];
RootViewController.m
#import "RootViewController.h" #import "ImageCell.h" #import "ImageURL.h" #define kImageCell @"imagecell" @interface RootViewController ()<UICollectionViewDataSource> @property(nonatomic,retain)NSMutableArray *dataSource;//存储model对象 @end
//释放
- (void)dealloc
{
self.dataSource = nil;
[super dealloc];
}
//懒加载
- (NSMutableArray *)dataSource{
if (_dataSource == nil) {
self.dataSource = [NSMutableArray arrayWithCapacity:0];
}
return [[_dataSource retain]autorelease];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//调用配置CollectionView
[self confgureCollectionView];
//调用解析
[self readDataFromFile];
}
解析数据:
- (void)readDataFromFile{
//获取文件的路径
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"Data.json" ofType:nil];
//使用文件的初始化NSData对象
NSData *data = [NSData dataWithContentsOfFile:filePath];
//使用json解析
NSMutableArray *sourceArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
// NSLog(@"%@",sourceArray);
//遍历字典
for (NSDictionary *dic in sourceArray) {
//创建model对象
ImageURL *URL = [[ImageURL alloc]init];
//添加到model
[URL setValuesForKeysWithDictionary:dic];
//添加到数组
[self.dataSource addObject:URL];
NSLog(@"%@",self.dataSource);
}
}
配置CollectionView
//配置CollectionView
- (void)confgureCollectionView{
//创建布局工具
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
//设置item的大小
flowLayout.itemSize = CGSizeMake(140, 160);
//设置分区缩进量
flowLayout.sectionInset = UIEdgeInsetsMake(5, 10, 5, 10);
//创建CollectionView对象
UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];
//配置数据源代理
collectionView.dataSource = self;
//注册cell
[collectionView registerClass:[ImageCell class] forCellWithReuseIdentifier:kImageCell];
//设置背景颜色
collectionView.backgroundColor = [UIColor whiteColor];
//添加到父视图上
[self.view addSubview:collectionView];
[collectionView release];
[flowLayout release];
}
#pragma mark 数据源代理方法
//返回分区个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.dataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
ImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kImageCell forIndexPath:indexPath];
//根据item的下标取出对应位置的数据
ImageURL *url = self.dataSource[indexPath.item];
//调用cell控件赋值的方法
[cell assignValueByImageURL:url];
return cell;
}
自定义cell:
<span style="font-size:24px;">//ImageCell.h
#import <UIKit/UIKit.h>
@class ImageURL;
@interface ImageCell : UICollectionViewCell
//写一个方法给cell上控件赋值
- (void)assignValueByImageURL : (ImageURL *)image;
@end
//ImageCell.m
#import "ImageCell.h"
#import "UIImageView+WebCache.h"
#import "ImageURL.h"
@interface ImageCell ()
@property(nonatomic,retain)UIImageView *photoView;
@end
@implementation ImageCell
- (void)dealloc
{
self.photoView = nil;
[super dealloc];
}
- (id)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self.contentView addSubview:self.photoView];
}
return self;
}
- (UIImageView *)photoView{
if (_photoView == nil) {
self.photoView = [[UIImageView alloc]initWithFrame:self.bounds];
self.photoView.backgroundColor = [UIColor cyanColor];
}
return [[_photoView retain]autorelease];
}
//写一个方法给cell上控件赋值
- (void)assignValueByImageURL : (ImageURL *)image{
//1.使用图片异步加载
[self.photoView sd_setImageWithURL:[NSURL URLWithString:image.thumbURL] placeholderImage:[UIImage imageNamed:@"占位1"]];
}
@end</span>
建一个model数据类:
<span style="font-size:24px;">//ImageURL.h
@interface ImageURL : NSObject
@property(nonatomic,copy)NSString *thumbURL;
@end
//ImageURL.m
#import "ImageURL.h"
@implementation ImageURL
- (void)dealloc
{
self.thumbURL= nil;
[super dealloc];
}
//防止Crash
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
}
@end</span>
效果展示:
图片占位:
------------------------------------------------------------------
Data文件下载:http://pan.baidu.com/s/1ntw5W3f
本节知识点:http://blog.csdn.net/qq_31810357/article/details/49154985
UICollectionView请求网络数据显示(Text)的更多相关文章
- react-native 项目实战 -- 新闻客户端(4) -- 请求网络数据
1.Home.js /** * 首页 */ import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Te ...
- Android请求网络共通类——Hi_博客 Android App 开发笔记
今天 ,来分享一下 ,一个博客App的开发过程,以前也没开发过这种类型App 的经验,求大神们轻点喷. 首先我们要创建一个Andriod 项目 因为要从网络请求数据所以我们先来一个请求网络的共通类. ...
- Android 手机卫士--构建服务端json、请求网络数据
本文地址:http://www.cnblogs.com/wuyudong/p/5900384.html,转载请注明源地址. 数据的传递 客户端:发送http请求 http://www.oxx.com/ ...
- 安卓中自定义并使用Volley框架请求网络
大家好,今天我们讲一下如何使用Volley框架请求网络,为何要使用Volley框架,这就要先说一下使用Volley框架请求网络的优点了,volley是易于定制的,即你可以根据需求来设定volley框架 ...
- 使用innerHTML生成的script节点不会发出请求与执行text属性
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 在Service服务中请求网络
一.startservice方式启动 第一次startservice启动服务的时候,会走oncreate和onstart方法, 第二次startservice启动服务的时候,会走onstart方法, ...
- Android - 使用Volley请求网络数据
Android - 使用Volley请求网络数据 Android L : Android Studio 14 个人使用volley的小记,简述使用方法,不涉及volley源码 准备工作 导入Volle ...
- 解决React Native使用Fetch API请求网络报Network request failed
问题来源: 1 . 在测试fetch数据请求时,Xcode9.0以上的无法请求https, 需要在Xcode中加载项目后修改Info.plist的相关配置,具体如下参考 问题及解决方法一模一样,不再重 ...
- 安卓请求网络错误 直接在main Thread 进行网络操作出现maintreamexception
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectDiskReads().detectDiskWrites ...
随机推荐
- css3部分整理
1.css弹性盒子属性 父级元素属性的设置 #father{ width: 800px; height: 300px; background-color: darkgray; /*定义父级元素为弹性元 ...
- 移动端web开发中对点透的处理,以及理解fastclick如何做到去除300ms延迟
一.点透问题以及处理办法 开发中遇到一个问题,就是点击layer弹出框的取消按钮之后,按钮下方的click事件就直接触发了.直接看代码: $('.swiper-slide').on('click', ...
- gdb调试的基本使用
GDB调试 启动程序准备调试 GDB yourpram 或者 先输入GDB 然后输入 file yourpram 然后使用run或者r命令开始程序的执行,也可以使用 run parameter将参数传 ...
- CRM客户关系管理系统(五)
第五章.分页功能开发 5.1.修改BaseKingAdmin和完善前段页面显示 现在访问没有注册的model会报错,因为基类中没有写list_display和list_filter. 在基类中设置一个 ...
- JavaScript 比较和逻辑运算符
比较和逻辑运算符用于测试 true 或者 false. 比较运算符 比较运算符在逻辑语句中使用,以测定变量或值是否相等. 给定 x=5,下面的表格解释了比较运算符: 实例 »实例 » 大于 大于或等于 ...
- Nginx之(四)工作原理
众所周知,nginx性能高,而nginx的高性能与其架构是分不开的 4.1 进程模型 Nginx在启动后,会有一个master进程和多个worker进程.master进程主要用来管理worker进程, ...
- JVM初探- 内存分配、GC原理与垃圾收集器
JVM初探- 内存分配.GC原理与垃圾收集器 标签 : JVM JVM内存的分配与回收大致可分为如下4个步骤: 何时分配 -> 怎样分配 -> 何时回收 -> 怎样回收. 除了在概念 ...
- 浅析"Sublabel-Accurate Relaxation of Nonconvex Energies" CVPR 2016 Best Paper Honorable Mention
今天作了一个paper reading,感觉论文不错,马克一下~ CVPR 2016 Best Paper Honorable Mention "Sublabel-Accurate Rela ...
- Scala:集合类型Collection和迭代器
http://blog.csdn.net/pipisorry/article/details/52902549 Scala Collection Scala 集合分为可变的和不可变的集合. 可变集合可 ...
- 网卡配置和DNS配置,手动挂在nas存储的共享目录,网络相关其它操作命令,修改防火墙中的端口配置,resolv.conf配置详细介绍和网卡信息配置详细介绍
1. 网卡配置和DNS配置 若想服务器能够发邮件,需要让部署的服务器能够访问到外网环境.若部署的服务器访问不到外网,通过ping www.baidu.com的方式执行的时候,会出现以下问题: &q ...