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.****.net/qq_31810357/article/details/49154985