#import "ViewController.h"
#import "CollectionViewCell.h"
#define COL_NUM 3
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (strong,nonatomic) NSArray *events;
@property (strong,nonatomic) UICollectionView *collectionView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"events" ofType:@"plist"];
self.events = [[NSArray alloc] initWithContentsOfFile:plistPath];
[self setupCollectionView];
}
-(void)setupCollectionView{
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
layout.itemSize = CGSizeMake(80, 80);
layout.sectionInset = UIEdgeInsetsMake(15, 15, 30, 15);
CGSize screenSize = [UIScreen mainScreen].bounds.size;
if (screenSize.height>586) {
layout.itemSize = CGSizeMake(100, 100);
layout.sectionInset = UIEdgeInsetsMake(15, 15, 20, 15);
}
layout.minimumInteritemSpacing = 10;
self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:layout];
[self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
self.collectionView.backgroundColor = [UIColor whiteColor];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.view addSubview:self.collectionView];
}
//datasource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
int num = [self.events count] % COL_NUM;
if (num==0) {
return [self.events count]/COL_NUM;
}else{
return [self.events count]/COL_NUM+1;
}
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return COL_NUM;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
NSInteger idx = indexPath.section * COL_NUM + indexPath.row;
if (self.events.count <= idx) {
return cell;
}
NSDictionary *event = self.events[idx];
cell.label.text = [event objectForKey:@"name"];
cell.imageView.image = [UIImage imageNamed:event[@"image"]];
return cell;
}
//delegate
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSDictionary *event = self.events[indexPath.section * COL_NUM + indexPath.row];
NSLog(@"select event name:%@",event[@"name"]);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import <UIKit/UIKit.h>
@interface CollectionViewCell : UICollectionViewCell
@property (strong,nonatomic) UIImageView *imageView;
@property (strong,nonatomic) UILabel *label;
@end
#import "CollectionViewCell.h"
@implementation CollectionViewCell
-(id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
//cellWidth
CGFloat cellWidth = self.frame.size.width;
//imageView
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake((cellWidth-101)/2, 15, 101, 101)];
[self addSubview:self.imageView];
//label
self.label = [[UILabel alloc] initWithFrame:CGRectMake((cellWidth-101)/2, 120, 101, 16)];
self.label.textAlignment = NSTextAlignmentCenter;
self.label.font = [UIFont systemFontOfSize:13];
[self addSubview:self.label];
}
return self;
}
@end