ViewController.m
#import "ViewController.h"
#import "Cell.h"
#import "UIImageView+WebCache.h"
#import "MyCollectionReusableView.h"
@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic, retain)NSMutableArray *arr;
@end
@implementation ViewController
- (void)dealloc
{
[_arr release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.itemSize = CGSizeMake(100, 120);
flowLayout.minimumLineSpacing = 1;
flowLayout.minimumInteritemSpacing = 1;
flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
flowLayout.headerReferenceSize = CGSizeMake(200, 100);
UICollectionView *collectionV = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout];
[self.view addSubview:collectionV];
[collectionV release];
collectionV.backgroundColor = [UIColor yellowColor];
collectionV.dataSource = self;
collectionV.delegate = self;
[collectionV registerClass:[Cell class] forCellWithReuseIdentifier:@"reuse"];
[collectionV registerClass:[MyCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
[self createData];
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
if (kind == UICollectionElementKindSectionHeader) {
MyCollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerView" forIndexPath:indexPath];
reusableView.backgroundColor = [UIColor cyanColor];
reusableView.label.text = @"1";
return reusableView;
}
return nil;
}
- (void)createData {
NSString *path = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"json"];
NSData *data = [NSData dataWithContentsOfFile:path];
self.arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.arr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor blueColor];
cell.myLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row];
[cell.myImageView sd_setImageWithURL:[NSURL URLWithString:self.arr[indexPath.row][@"thumbURL"]]];
return cell;
}
Cell.h
#import <UIKit/UIKit.h>
@interface Cell : UICollectionViewCell
@property(nonatomic, retain)UILabel *myLabel;
@property(nonatomic, retain)UIImageView *myImageView;
@end
#import "Cell.h"
@implementation Cell
- (void)dealloc
{
[_myImageView release];
[_myLabel release];
[super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[self.contentView addSubview:self.myImageView];
[_myImageView release];
self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 100, 20)];
[self.contentView addSubview:self.myLabel];
[_myLabel release];
}
return self;
}
@end
MyCollectionReusableView.h
#import <UIKit/UIKit.h>
@interface MyCollectionReusableView : UICollectionReusableView
@property(nonatomic, retain)UILabel *label;
@end
#import "MyCollectionReusableView.h"
@implementation MyCollectionReusableView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.label = [[UILabel alloc] init];
[self addSubview:self.label];
[_label release];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
self.label.frame = CGRectMake(0, 50, 100, 30);
}
@end