集合视图UICollectionView

#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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值