UI3_UICollectionViewMuti
// AppDelegate.m
// UI3_UICollectionViewMuti
//
// Created by zhangxueming on 15/7/16.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "AppDelegate.h"
#import "ViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
ViewController *root = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:root];
self.window.rootViewController = nav;
self.window.backgroundColor = [UIColor whiteColor]; return YES;
}
// ViewController.h
// UI3_UICollectionViewMuti
//
// Created by zhangxueming on 15/7/16.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import <UIKit/UIKit.h> @interface ViewController : UIViewController @end //
// ViewController.m
// UI3_UICollectionViewMuti
//
// Created by zhangxueming on 15/7/16.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "ViewController.h"
#import "DataModel.h"
#import "HeaderCollectionReusableView.h"
#import "FooterCollectionReusableView.h"
#import "DataCollectionViewCell.h" #define kCellReuseID @"cellId" #define kHeaderReuseID @"headerId"
#define kFooterReuseId @"footerId" //UICollectionViewDelegateFlowLayout 遵守 UICollectionViewDelegate
@interface ViewController () <UICollectionViewDelegateFlowLayout,UICollectionViewDataSource>
{
UICollectionView *_collectionView;
NSMutableArray *_dataList;
} @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//创建数据源
[self createDatadList];
//创建UI
[self createUI];
//代理
} - (void)createDatadList
{
_dataList = [NSMutableArray array];
for (int i=0; i<4; i++) {
NSMutableArray *array = [NSMutableArray array];
for (int j=0; j<5; j++) {
NSString *name = [NSString stringWithFormat:@"picture%d",i*5+j];
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"png"];
DataModel *model = [[DataModel alloc] init];
model.title = [NSString stringWithFormat:@"图片%d",i*5+j];
model.image = [UIImage imageWithContentsOfFile:path];
[array addObject:model];
}
[_dataList addObject:array];
}
} - (void)createUI
{
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
_collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
_collectionView.backgroundColor = [UIColor whiteColor]; //注册cell [_collectionView registerClass:[DataCollectionViewCell class] forCellWithReuseIdentifier:kCellReuseID];
//注册headerView footerView
//注册headerView
//UICollectionElementKindSectionHeader headerView 类型
[_collectionView registerClass:[HeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kHeaderReuseID];
//注册footerView
[_collectionView registerClass:[FooterCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kFooterReuseId]; //设置代理
_collectionView.delegate = self;
_collectionView.dataSource = self;
[self.view addSubview:_collectionView];
} #pragma mark ---UICollectionViewDataSource--- //返回分区的个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return _dataList.count;
} //返回cell的个数 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [_dataList[section] count];
} //返回cell - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
DataCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellReuseID forIndexPath:indexPath];
DataModel *model = [[_dataList objectAtIndex:indexPath.section] objectAtIndex:indexPath.item];
[cell config:model];
return cell;
} #pragma mark ---UICollectionViewDelegateFlowLayout---
//返回cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(150, 120);
} //返回上下左右边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(10, 5, 10, 5);
} //返回竖向的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 20;
}
//返回横向的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 5;
} //获取headerView footerView - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
//headerView
if (kind == UICollectionElementKindSectionHeader)
{
HeaderCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:kHeaderReuseID forIndexPath:indexPath];
headerView.titleLabel.text = [NSString stringWithFormat:@"第%ld分区", indexPath.section];
headerView.titleLabel.textAlignment = NSTextAlignmentCenter;
headerView.imageView.image = [UIImage imageNamed:@"headerImage.png"];
headerView.backgroundColor = [UIColor yellowColor];
return headerView;
}//footerView
else if(kind == UICollectionElementKindSectionFooter)
{
FooterCollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:kFooterReuseId forIndexPath:indexPath];
footerView.imageView.image = [UIImage imageNamed:@"footerImage@2x"];
return footerView;
}
return nil;
} //返回headerView大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
return CGSizeMake(375, 44);
} //返回footerView的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
return CGSizeMake(375, 44);
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
// HeaderUICollectionReusableView.h
// UI3_UICollectionViewMuti
//
// Created by zhangxueming on 15/7/16.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import <UIKit/UIKit.h> @interface HeaderCollectionReusableView : UICollectionReusableView
//{
// UILabel *_titleLabel;
// UIImageView *_imageView;
//} @property (nonatomic, retain)UILabel *titleLabel;
@property (nonatomic, retain)UIImageView *imageView; @end //
// HeaderUICollectionReusableView.m
// UI3_UICollectionViewMuti
//
// Created by zhangxueming on 15/7/16.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "HeaderCollectionReusableView.h" @implementation HeaderCollectionReusableView //重写HeaderCell
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(300, 0, 44, 44)];
[self addSubview:_titleLabel];
[self addSubview:_imageView];
}
return self;
} @end
// FooterCollectionReusableView.h
// UI3_UICollectionViewMuti
//
// Created by zhangxueming on 15/7/16.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import <UIKit/UIKit.h> @interface FooterCollectionReusableView : UICollectionReusableView
//{
// UIImageView *_imageView;
//} @property (nonatomic, retain)UIImageView *imageView; @end // FooterCollectionReusableView.m
// UI3_UICollectionViewMuti
//
// Created by zhangxueming on 15/7/16.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "FooterCollectionReusableView.h" @implementation FooterCollectionReusableView //重写FooterCell
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(00, 0, 375, 44)];
[self addSubview:_imageView];
}
return self;
} @end
// DataCollectionViewCell.h
// UI3_UICollectionViewMuti
//
// Created by zhangxueming on 15/7/16.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import <UIKit/UIKit.h>
#import "DataModel.h" @interface DataCollectionViewCell : UICollectionViewCell
{
UILabel *_titleLabel;
UIImageView *_imageView;
} //显示模型数据
- (void)config:(DataModel *)model; @end //
// DataCollectionViewCell.m
// UI3_UICollectionViewMuti
//
// Created by zhangxueming on 15/7/16.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "DataCollectionViewCell.h" @implementation DataCollectionViewCell - (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_titleLabel = [[UILabel alloc] init];
_imageView = [[UIImageView alloc] init];
[self addSubview:_imageView];
[_imageView addSubview:_titleLabel];
}
return self;
} //显示数据
- (void)config:(DataModel *)model
{
_titleLabel.frame = CGRectMake(0, 0, 150, 30);
_imageView.frame = CGRectMake(0, 0, 150, 100);
_titleLabel.textAlignment = NSTextAlignmentCenter;
_titleLabel.backgroundColor = [UIColor grayColor];
_titleLabel.alpha = 0.5;
_titleLabel.textColor = [UIColor greenColor];
_titleLabel.text = model.title;
_imageView.image = model.image;
} @end
// DataModel.h
// UI3_UICollectionViewMuti
//
// Created by zhangxueming on 15/7/16.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> @interface DataModel : NSObject @property (nonatomic,copy) NSString *title;
@property (nonatomic,strong) UIImage *image; @end // DataModel.m
// UI3_UICollectionViewMuti
//
// Created by zhangxueming on 15/7/16.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "DataModel.h" @implementation DataModel @end
UI3_UICollectionViewMuti的更多相关文章
随机推荐
- Ubuntu:Target filesystem doesn't have /sbin/init (Slax 解决)
计算机(Ubuntu)因为异常断电或是其它原因,再次启动时.非常不幸的出现: Killed mount: mounting /dev on /root/dev failed: No such file ...
- 发现js端日期跟php端日期格式不一致
当程序没有问题,而php显示的日期和js显示的日期不一致,相差一天的时候或者其它,一定要看看php.ini中时区的配置是否合理.
- 一、List的扩展
前言 List的三个扩展方法 1.处理符合in()格式的扩展方法 2.处理符合SelectListItem类型的扩展方法(SelectListItem类型是下拉框要用到的类型) 3.Distinct方 ...
- 关于CQRS(老外经典好文)
CQRS means Command Query Responsibility Segregation. Many people think that CQRS is an entire archit ...
- [android]fmodex在某些android设备上声音延迟Latency
// The default on windows in bufferlength = 1024 and numbuffers = 4 hr = g_pFmodSystem->setDSPBuf ...
- 项目源码--Android聚合视频类播放器
下载源码 技术要点: 1.高效支持主流的视音频格式 2.本地视频的播放与管理 3.聚合电视在线直播 4.聚合优酷.搜狐.乐视.爱奇艺等多种在线视频 5.优质播放,包含播放.暂停,声音.亮度调整等功能 ...
- A*算法解决八数码问题 Java语言实现
0X00 定义 首先要明确一下什么是A*算法和八数码问题? A*(A-Star)算法是一种静态路网中求解最短路径最有效的直接搜索方法也是一种启发性的算法,也是解决许多搜索问题的有效算法.算法中的距离估 ...
- SQL Server 表和索引存储结构
在上一篇文章中,我们介绍了SQL Server数据文件的页面类型,系统通过96个字节的头部信息和系统表从逻辑层面上将表的存储结构管理起来,具体到表的存储结构上,SQL Server引入对象.分区.堆或 ...
- a letter and a number
描述we define f(A) = 1, f(a) = -1, f(B) = 2, f(b) = -2, ... f(Z) = 26, f(z) = -26;Give you a letter x ...
- 测试你是否和LTC水平一样高
Problem Description 大家提到LTC都佩服的不行,不过,如果竞赛只有这一个题目,我敢保证你和他绝对在一个水平线上!你的任务是:计算方程x^2+y^2+z^2= num的一个正整数解. ...