一、定义我们的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开发指南 !
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
网站题目:CollectionView纯代码手敲-创新互联
本文链接:https://www.cdcxhl.com/article30/pihpo.html
成都网站建设公司_创新互联,为您提供网站排名、网站制作、营销型网站建设、云服务器、企业建站、商城网站
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联