本文转载至 http://blog.csdn.net/daleiwang/article/details/40423219

虽然这个早已不是新东西了,但是之前项目中一直没有机会用,只知道跟tableView原理相同。

弄了个自定义UICollectionViewCell的小DEMO:

(1)在storyboard中拖拽一个UICollectionViewController:

(2)新建RootCollectionViewController继承自UICollectionViewController

  1. #import "RootCollectionViewController.h"
  2. #import "RootCollectionViewCell.h"
  3. @interface RootCollectionViewController ()
  4. @end
  5. @implementation RootCollectionViewController
  6. - (void)viewDidLoad {
  7. [super viewDidLoad];
  8. self.clearsSelectionOnViewWillAppear = NO;
  9. }
  10. - (void)didReceiveMemoryWarning {
  11. [super didReceiveMemoryWarning];
  12. }
  13. #pragma mark <UICollectionViewDataSource>
  14. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  15. return 1;
  16. }
  17. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  18. return 20;
  19. }
  20. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  21. static NSString * CellIdentifier = @"GradientCell";
  22. RootCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
  23. cell.backgroundColor = [UIColor colorWithRed:((110 * indexPath.row) / 255.0) green:((220 * indexPath.row)/255.0) blue:((330 * indexPath.row)/255.0) alpha:1.0f];
  24. [cell.lab setText:@"xxxx"];
  25. [cell.frontView setBackgroundColor:[UIColor redColor]];
  26. return cell;
  27. }
  28. #pragma mark <UICollectionViewDelegate>
  29. @end

(3)新建RootCollectionViewCell继承自UICollectionViewCell,头文件如下,自定义两个控件

  1. #import <UIKit/UIKit.h>
  2. @interface RootCollectionViewCell : UICollectionViewCell
  3. @property(nonatomic,weak)IBOutlet UILabel *lab;
  4. @property(nonatomic,weak)IBOutlet UIView *frontView;
  5. @end

(4)storyboard操作:

点击CollectionViewController,设置Class:

点击CollectionCell设置Class:

拖动一个View和lab到CollectionView上,设置约束为固定宽高上下居中:

点击左侧的CollectionCell,,将IBOutlet,连接到lab和frontView上:

设置大小及边距,当然了,xcode6以后,也可以根据不同的屏幕设置相应SizeClass下的边距:

(5)运行效果:

UICollectionViewController xcode6.1 自定义Cell的更多相关文章

  1. iOS 中使用 XIB 自定义cell 的两种方法 以及 编译出现常见 的错误 ++++(xcode6.0之后)

    一. 注册cell 1.创建自定义cell并勾选 xib :(勾选xib就会自动生成与cell文件关联的xib) 2.在 tableViewController里注册自定义Cell (或者遵守tabl ...

  2. iOS 中使用 XIB 自定义cell的两种方法以及编译出现常见 的错误 (xcode6.0之后)

    一. 注册cell 1.创建自定义cell并勾选 xib :(勾选xib就会自动生成与cell文件关联的xib) 2.在 tableViewController里注册自定义Cell (或者遵守tabl ...

  3. 自定义cell自适应高度

    UITableView在许多App种被大量的应用着,呈现出现的效果也是多种多样的,不能局限于系统的一种样式,所以需要自定义cell 自定义cell呈现的内容也是多种多样的,内容有多有少,所以需要一种能 ...

  4. 自定义cell(xib)中button点击事件不能响应的情况

    遇到这种问题真的好尴尬,之前从来没有遇到过,以为手到擒来,未曾料到还会遇到问题! 好多年没有找到尴尬的感觉,现在找到了,真的很尴尬 !  *o* 1.首先使用场景: 原本没打算用xib,后来为了快速, ...

  5. ios中自定义cell 设置cell的分组结构

    ios系统默认的cell并不能满足我们的需求 这个时候就需要自定义我们的cell 自定义cell为分组的时候 需要设置分组样式  以下是我常用分组的二种方法: 第一是 在自定义的UITableView ...

  6. iOS开发小技巧--纯代码自定义cell

    纯代码自定义cell 自定义cell的步骤(每个cell的高度不一样,每个cell里面显示的内容也不一样) 1.新建一个继承自UITableViewCell的子类 2.在initWithStyle:方 ...

  7. 自定义cell的一些知识

    1.要往cell里面添加一个自定义的子控件,都是添加到cell的contentView,不是添加到cell里面. 2.通过xib自定义cell * 添加tableView * 加载团购数据 * 新建x ...

  8. 给自定义cell赋值

    搭建自定义cell-给自定义cell赋值的思路 1 主控制器 1.1导入头文件 #import "LHQInvestmentManagementCell.h" #import &q ...

  9. 自定义cell

    思路就是创建模型,自定义cell,然后在主控制器中完成,首先要观察plist文件: Contact.h #import <Foundation/Foundation.h> @interfa ...

随机推荐

  1. 九度oj 题目1099:后缀子串排序

    题目描述: 对于一个字符串,将其后缀子串进行排序,例如grain其子串有:grain rain ain in n 然后对各子串按字典顺序排序,即: ain,grain,in,n,rain 输入: 每个 ...

  2. [luoguP4035] [JSOI2008]球形空间产生器(高斯消元)

    传送门 设球心的坐标为未知量 用最后一个点来表示球面到球心的距离,那么它和前n个式子相等 移项乱搞 最后高斯消元 #include <cmath> #include <cstdio& ...

  3. jQuary的相关动画效果

    第一种:该方法隐藏所有 <p> 元素: <html> <head> <script type="text/javascript" src= ...

  4. bzoj3743 [Coci2015]Kamp 常州模拟赛d6t2

    3743: [Coci2015]Kamp Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 484  Solved: 229[Submit][Status ...

  5. 如何快速下载maven依赖jar包

    找到settings.xml文件.在mirrors里面添加下面的代码: <mirror> <id>alimaven</id> <mirrorOf>cen ...

  6. 【HDOJ5952】Counting Cliques(团,dfs)

    题意:给定一张n点m边的图,求大小为S的团的个数 N ≤ 100,M ≤ 1000,2 ≤ S ≤ 10,保证点的度不超过20 思路:dfs 因为每个点可能不止属于一个极大团,所以不能求出极大团然后计 ...

  7. 插头DP--URAL1519Formula 1

    去年的老朋友.挺怀念的,回来看看. $n \leq 12,m \leq 12$,$n*m$的01矩形,问在0中走路的欧拉回路数.答案保证longlong范围. 先设计插头:左右括号和空插头:然后分3* ...

  8. MongoDB_起步

    MongoDB基本概念 <1> mogoDB是一个文档存储类型的nosql数据库,文档存储一般用类似json的格式存储,存储的内容是文档型的. 这样也就有机会对某些字段建立索引, < ...

  9. hdu 1754 splay tree伸展树 初战(单点更新,区间属性查询)

    题意:与区间查询点更新,点有20W个,询问区间的最大值.曾经用线段树,1000+ms,今天的伸展树,890没ms,差不多. 第一次学习伸展树,一共花了2个单位时间,感觉伸展树真很有用,也很好玩.现在只 ...

  10. 连接mysql报错 : The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone...

    time zone 时区错误 DBEAVER连接MySQL运行报错The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or repres ...