UICollectionView请求网络数据显示(Text)
来源:互联网 发布:霍先生的安之知百度云 编辑:程序博客网 时间:2024/04/28 05:42
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;}
<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
0 0
- UICollectionView请求网络数据显示(Text)
- OkHttp封装+RecyclerView显示网络请求数据
- 封装请求网络数据 显示XListView 加多条目展示
- tablayout上显示网络数据(xUtils请求)s
- 网络请求数据 保存数据库 查询数据库 显示
- android studio请求网络数据日志显示不全
- Retrofit网络请求+Recyclerview展示数据+Fresco显示图片
- Retrofit网络请求+Recyclerview展示数据+Fresco显示图片
- 网络数据请求
- qml 请求网络数据
- 网络请求数据解析
- iOS网络数据请求
- 请求网络数据JSON
- 网络请求数据
- 网络请求数据
- 网络数据请求
- 网络异步请求数据
- IOS_UI_网络数据请求
- X11: Linux跨网络运行XWindow程序
- iOS9 URLScheme白名单
- linux进程(二)之基础
- C++ 静态局部变量用法详解
- Linux LVM硬盘管理及LVM扩容
- UICollectionView请求网络数据显示(Text)
- 关于SandBox机制及文件读写的几种方式
- hdu 2011
- yii2 提示Module not inserted due to validation error.
- 实战分析一个崩溃的bug (2010-10-25 01:51:24)
- POJ 2718Smallest Difference(DFS)
- mybatis在xml文件中处理大于号小于号的方法
- 按钮背景图片 大小 和 位置 设置。
- 全局404定义