1.自定义单元格

#import <UIKit/UIKit.h>

@interface myCollectionViewCell : UICollectionViewCell
@property(nonatomic,strong) UIImageView *myImageView;
@property(nonatomic,strong) UILabel *nameLabel;
@end
#import "myCollectionViewCell.h"

@implementation myCollectionViewCell
-(void)layoutSubviews
{
    self.myImageView.frame=CGRectMake(, , self.contentView.frame.size.width,);
    [self.contentView addSubview:self.myImageView];

    self.nameLabel.frame = CGRectMake(, , self.contentView.frame.size.width, );
    [self.contentView addSubview:self.nameLabel];
    [self.nameLabel setBackgroundColor:[UIColor cyanColor]];

}
@end

2.设置UICollectionViewFlowLayout

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    UICollectionViewFlowLayout *collectionViewFlowLayout=[[UICollectionViewFlowLayout alloc]init];
    //每行内部cell item的间距
    collectionViewFlowLayout.minimumInteritemSpacing=10.0;
//     每行的间距
    collectionViewFlowLayout.minimumLineSpacing=20.0;
    collectionViewFlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    collectionViewFlowLayout.sectionInset = UIEdgeInsetsMake(, 0.0);
    //它定义了每一个item的大小。通过设定itemSize可以全局地改变所有cell的尺寸,如果想要对某个cell制定尺寸,可以使用-collectionView:layout:sizeForItemAtIndexPath:方法
    collectionViewFlowLayout.itemSize=CGSizeMake(, );
    collectionViewFlowLayout.estimatedItemSize=CGSizeMake(, );
    //由属性scrollDirection确定scroll view的方向,将影响Flow Layout的基本方向和由header及footer确定的section之间的宽度
    collectionViewFlowLayout.scrollDirection=UICollectionViewScrollDirectionVertical;
    //Header和Footer尺寸 同样地分为全局和部分。需要注意根据滚动方向不同,header和footer的高和宽中只有一个会起作用。垂直滚动时section间宽度为该尺寸的高,而水平滚动时为宽度起作用
    collectionViewFlowLayout.headerReferenceSize=CGSizeMake(, );
    collectionViewFlowLayout.footerReferenceSize=CGSizeMake(, );
    CollectionViewController *cvc=[[CollectionViewController alloc]initWithCollectionViewLayout:collectionViewFlowLayout];
    UINavigationController *nvc=[[UINavigationController alloc]initWithRootViewController:cvc];
    self.window.backgroundColor=[UIColor whiteColor];
    self.window.rootViewController=nvc;
    [self.window makeKeyAndVisible];

    return YES;
}

3.实现代理方法

//
//  CollectionViewController.m
//  collectionVC
//
//  Created by City--Online on 15/6/2.
//  Copyright (c) 2015年 CYW. All rights reserved.
//

#import "CollectionViewController.h"
#import "myCollectionViewCell.h"
@interface CollectionViewController ()

@end

@implementation CollectionViewController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.collectionView registerClass:[myCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer"];

   }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    ;

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    ) {
        ;
    }
    else
    {
        ;
    }

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    myCollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier  forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithRed:arc4random()%/  /  / ];
    cell.myImageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1.png"]];
    cell.nameLabel=[[UILabel alloc]init];
    cell.nameLabel.text=[NSString stringWithFormat:@"%ld %ld",indexPath.section,indexPath.row];
    return cell;
}

#pragma mark <UICollectionViewDelegate>

// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {

}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString: UICollectionElementKindSectionFooter]) {
        UICollectionReusableView *footer=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Footer" forIndexPath:indexPath];
        footer.backgroundColor=[UIColor yellowColor];
        UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(, , , )];
        label.text=@"label";
        label.textAlignment=NSTextAlignmentCenter;
        [footer addSubview:label];
        return footer;
    }
    else
    {
        UICollectionReusableView *Header=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Header" forIndexPath:indexPath];
        Header.backgroundColor=[UIColor blueColor];
        return Header;

    }
}

@end

4.运行结果图

UIKit 框架之UICollectionViewController的更多相关文章

  1. UIKit框架使用总结--看看你掌握了多少

    一.经常使用的,基本就是每次项目迭代都需要使用的 UIView.UILabel.UIImage.UIColor.UIFont.UIImageView.UITextField.UIButton. UIS ...

  2. Swift - 重写UIKit框架类的init初始化方法(以UITabBarController为例)

    原来写了篇文章讲UITabBarController的用法,当时是从UIViewController跳转到UITabBarController页面,代码如下: 1 self.presentViewCo ...

  3. UIKit框架

    在今后的应用程序构建中,会陆续使用各式各样的控件,因此UIKit框架的引入是必不可少的! 一.简介 UIKitk框架提供一系列的Class(类)来建立和管理iPhone OS应用程序的用户界面接口.应 ...

  4. iOS学习32之UIKit框架-可视化编程-XIB

    1. Interface Builder 可视化编程 1> 概述 GUI : 图形用户界面(Graphical User Interface, 简称GUI, 又称图形化界面) 是指采用图形方式显 ...

  5. 基础框架Fundation和UIkit框架的定义和使用

    Foundation 框架为所有应用程序提供基本的系统服务 您的应用程序以及 UIKit 和其他框架,都建立在 Foundation 框架的基础结构之上.Foundation 框架提供许多基本的对象类 ...

  6. iOS开发概述UIkit动力学,讲述UIKit的Dynamic特性,UIkit动力学是UIkit框架中模拟真实世界的一些特性。

    转发:http://my.oschina.net/u/1378445/blog/335014 iOS UIKit动力学 Dynamics UIAttachmentBehavior 实现iMessage ...

  7. iOS开发UIKit框架-可视化编程-XIB

    1. Interface Builder 可视化编程 1> 概述 GUI : 图形用户界面(Graphical User Interface, 简称GUI, 又称图形化界面) 是指采用图形方式显 ...

  8. 79、iOS 的Cocoa框架、Foundation框架以及UIKit框架

    Cocoa框架是iOS应用程序的基础 1. Cocoa是什么? Cocoa是 OS X和ios 操作系统的程序的运行环境. 是什么因素使一个程序成为Cocoa程序呢?不是编程语言,因为在Cocoa开发 ...

  9. UIKit 框架之UIView二

    下面这些都是UIView一些基本的东西,具体的可以参考UIKit 框架之UIView一博客 一.自定义一个View // // MyView.m // UIView // // Created by ...

随机推荐

  1. 可自定义导航条功能案例ios项目源码

    可定制的navigationitem ,当我们使用系统的方法设置navigationItem的leftItem或者rightItem时,我们会 发现item位置会有偏差(左边的偏右,右边的偏左).当设 ...

  2. (四)、 nodejs中Async详解之一:流程控制

    为了适应异步编程,减少回调的嵌套,我尝试了很多库.最终觉得还是async最靠谱. 地址:https://github.com/caolan/async Async的内容分为三部分: 流程控制:简化十种 ...

  3. NSBundle UIImageView &UIButton

    1.NSBundle 1> 一个NSBundle代表一个文件夹,利用NSBundle能访问对应的文件夹 2> 利用mainBundle就可以访问软件资源包中的任何资源 3> 模拟器应 ...

  4. luigi学习-luigi的配置文件

    一.luigi配置文件的加载顺序 /etc/luigi/client.cfg luigi.cfg LUIGI_CONFIG_PATH环境变量 二.配置文件分节 配置文件被分为了多个section,每一 ...

  5. 【easyui】--combobox--赋值和获取选中的值

    //初始化下拉选框 $('#communityIdDiv').combobox({ url:basepath+"pushController/queryCommonityName" ...

  6. 网页绘制图表 Google Charts with JavaScript #1....好强、好简单啊!

    此为文章备份,原文出处(我的网站) 网页绘制图表 Google Charts with JavaScript....好强.好简单啊!#1 http://www.dotblogs.com.tw/mis2 ...

  7. Oracle 查看相关优化器参数

    select x.ksppinm name, y.ksppstvl value, y.ksppstdf isdefault, decode(bitand(y.ksppstvf, 7), 1, 'MOD ...

  8. 第九章 管理类型(In .net4.5) 之 继承机制

    1. 概述 本章包括 设计和实现接口.创建和使用基类 以及 使用.net类库提供的标准接口. 2. 主要内容 2.1 设计和实现接口 一个接口包含公共的方法.属性.事件.索引器.类和结构都可以实现接口 ...

  9. python使用简单http协议来传送文件

    python使用简单http协议来传送文件!在ubuntu环境下,局域网内可以使用nc来传送文件,也可以使用基于Http协议的方式来下载文件我们可以使用python -m SimpleHTTPServ ...

  10. ruby on rails 实战(一)

    通过ruby on rails 一步一步搭建个人站点,涉及到的技术有:ruby,rails,javascript,jquery 操作系统:win7 IDE: rubymine 5.4. 第一步,下载安 ...