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)的更多相关文章

  1. react-native 项目实战 -- 新闻客户端(4) -- 请求网络数据

    1.Home.js /** * 首页 */ import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Te ...

  2. Android请求网络共通类——Hi_博客 Android App 开发笔记

    今天 ,来分享一下 ,一个博客App的开发过程,以前也没开发过这种类型App 的经验,求大神们轻点喷. 首先我们要创建一个Andriod 项目 因为要从网络请求数据所以我们先来一个请求网络的共通类. ...

  3. Android 手机卫士--构建服务端json、请求网络数据

    本文地址:http://www.cnblogs.com/wuyudong/p/5900384.html,转载请注明源地址. 数据的传递 客户端:发送http请求 http://www.oxx.com/ ...

  4. 安卓中自定义并使用Volley框架请求网络

    大家好,今天我们讲一下如何使用Volley框架请求网络,为何要使用Volley框架,这就要先说一下使用Volley框架请求网络的优点了,volley是易于定制的,即你可以根据需求来设定volley框架 ...

  5. 使用innerHTML生成的script节点不会发出请求与执行text属性

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. 在Service服务中请求网络

    一.startservice方式启动 第一次startservice启动服务的时候,会走oncreate和onstart方法, 第二次startservice启动服务的时候,会走onstart方法, ...

  7. Android - 使用Volley请求网络数据

    Android - 使用Volley请求网络数据 Android L : Android Studio 14 个人使用volley的小记,简述使用方法,不涉及volley源码 准备工作 导入Volle ...

  8. 解决React Native使用Fetch API请求网络报Network request failed

    问题来源: 1 . 在测试fetch数据请求时,Xcode9.0以上的无法请求https, 需要在Xcode中加载项目后修改Info.plist的相关配置,具体如下参考 问题及解决方法一模一样,不再重 ...

  9. 安卓请求网络错误 直接在main Thread 进行网络操作出现maintreamexception

    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectDiskReads().detectDiskWrites ...

随机推荐

  1. Kafka(转载)

    Kafka是由LinkedIn开发的一个分布式的消息系统,使用Scala编写,它以可水平扩展和高吞吐率而被广泛使用.目前越来越多的开源分布式处理系统如Cloudera.Apache Storm.Spa ...

  2. vuex存储和本地存储(localstorage、sessionstorage)的区别

    1.最重要的区别:vuex存储在内存,localstorage则以文件的方式存储在本地 2.应用场景:vuex用于组件之间的传值,localstorage则主要用于不同页面之间的传值. 3.永久性:当 ...

  3. Microsoft Visual Studio 2017 编译最新版 libuv 1.x 并且生成 LIB 和 DLL 两种模式

    以为昨天晚上编译通过就可以了,哪知道,早上编译DLL的一车的报错 今天开始逐个解决,终于把引用的问题一亿解决了,具体步骤如下 1 在 Windows 平台下编译出错,显示导出未定义,打开 uv-win ...

  4. 一起撸个简单粗暴的Tv应用主界面的网格布局控件(上)

    这一篇是真的隔了好久了~~,也终于可以喘口气来好好写博客了,这段时间实在是忙不过来了,迭代太紧.好,废话不多说,进入今天的主题. 效果 图一是Tv应用:当贝市场的主页 图二是咱自己撸的简单粗暴的 Tv ...

  5. Linux 虚存的性能问题

    虚存子系统是所有 UNIX 系统的核心组件.下面讨论虚存系统的实现及其对操作系统中几乎其他所有子系统的作用和影响.首先详细说明一些基本的内存管理问题:然后具体分析 Linux 操作系统如何实施虚存管理 ...

  6. java 里面保留字volatile及其与synchronized的区别

           锁提供了两种主要特性:互斥(mutual exclusion) 和可见性(visibility).互斥即一次只允许一个线程持有某个特定的锁,因此可使用该特性实现对共享数据的协调访问协议, ...

  7. UILabel 调整行间距

    /* 调整行间距 */ + (void)adjustLineSpacingOfLabel:(UILabel *)label to:(CGFloat)lineSpacing { NSString *te ...

  8. 在8X8的棋盘上分布着n个骑士,他们想约在某一个格中聚会。骑士每天可以像国际象棋中的马那样移动一次,可以从中间像8个方向移动(当然不能走出棋盘),请计算n个骑士的最早聚会地点和要走多少天。要求尽早聚会

    在8X8的棋盘上分布着n个骑士,他们想约在某一个格中聚会.骑士每天可以像国际象棋中的马那样移动一次,可以从中间像8个方向移动(当然不能走出棋盘),请计算n个骑士的最早聚会地点和要走多少天.要求尽早聚会 ...

  9. 转:LINUX/UNIX下的回车换行与WINDOWS下的区别

      今天,我总算搞清楚“回车”(carriage return)和“换行”(line feed)这两个概念的来历和区别了.在计算机还没有出现之前,有一种叫做电传打字机(Teletype Model 3 ...

  10. Java基础之枚举妙用

    对于枚举,初学Java的时候可能我们就已经接触过了,但是在毕业前,其实一直都不知道真正工作里面枚举是怎么用的,枚举有什么用?接下来,博主就介绍枚举在实际工作中的一种使用场景,本文只适合初级的小菜鸟看哈 ...