一、定义我们的CollectViewCell
// Tiny_CollectionViewCell.h
// Tiny_UICollectionViewController
//
// Created by Tiny on 15-6-16.
// Copyright (c) 2015年 Tiny. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface Tiny_CollectionViewCell : UICollectionViewCell
@property(strong, nonatomic) UIImageView *p_w_picpathView; //图片定义
@property(strong, nonatomic) UILabel *label; //标签定义
@end
二、实现我们的CollectViewCell:
//
// Tiny_CollectionViewCell.m
// Tiny_UICollectionViewController
//
// Created by Tiny on 15-6-16.
// Copyright (c) 2015年 Tiny. All rights reserved.
//
#import "Tiny_CollectionViewCell.h"
@implementation Tiny_CollectionViewCell
@synthesize p_w_picpathView;
@synthesize label;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
//初始化图片
self.p_w_picpathView = [[UIImageView alloc] init];
//定义图片frame
[self.p_w_picpathView setFrame:CGRectMake(25, 15, 101, 101)];
[self.contentView addSubview:self.p_w_picpathView];
//初始化标签
self.label = [[UILabel alloc] init];
//定义标签frame
[self.label setFrame:CGRectMake(35, 121, 150, 20)];
[self.contentView addSubview:self.label];
}
return self;
}
@end
三、定义我们的控制器ViewController:
//
// Tiny_ViewController.h
// Tiny_UICollectionViewController
//
// Created by Tiny on 15-6-16.
// Copyright (c) 2015年 Tiny. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface Tiny_ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property(strong, nonatomic) NSArray *events;
@property(strong, nonatomic) UICollectionView *collectionView;
@end
四、实现我们的控制器ViewController:
//
// Tiny_ViewController.m
// Tiny_UICollectionViewController
//
// Created by Tiny on 15-6-16.
// Copyright (c) 2015年 Tiny. All rights reserved.
//
#import "Tiny_ViewController.h"
#import "Tiny_CollectionViewCell.h"
@interface Tiny_ViewController ()
@end
@implementation Tiny_ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CGRect screenRect = [[UIScreen mainScreen] bounds];
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:@"events" ofType:@"plist"];
//获取属性列表文件中的全部数据
NSArray *array = [[NSArray alloc] initWithContentsOfFile:plistPath];
self.events = array;
UICollectionViewFlowLayout *layout= [[UICollectionViewFlowLayout alloc]init];
self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, screenRect.size.width, screenRect.size.height) collectionViewLayout:layout];
[self.collectionView registerClass:[Tiny_CollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.collectionView setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:self.collectionView];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return [self.events count] / 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 2;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
Tiny_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
NSDictionary *event = [self.events objectAtIndex:(indexPath.section * 2 + indexPath.row)];
cell.p_w_picpathView.p_w_picpath = [UIImage p_w_picpathNamed:[event objectForKey:@"p_w_picpath"]];
cell.label.text = [event objectForKey:@"name"];
return cell;
}
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *event = [self.events objectAtIndex:(indexPath.section*2 + indexPath.row)];
UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"选择项目" message:[NSString stringWithFormat:@"你选了%@项目", [event objectForKey:@"name"]] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[a show];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(140, 140);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
五、导入events.plist文件(内容大概如下)
六、完美运行
七、 资源参考 关老师写的iOS开发指南 !
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。