本文转载至 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. OkHttpUtil

    package jp.co.gunmabank.util import android.os.Handlerimport android.os.Looperimport com.google.gson ...

  2. 对拍程序(Win)

    代码如下: @echo off :again rand.exe echo "rand finish" asd.exe echo "1.exe finish" 未 ...

  3. javaScriptCore 实战与小结

      源码在这,看不懂的直接撸源码就行,转载声明出处 原生调用JS的大致流程,做了个思维简图 这是代码流程 // JS数据 func getJSVar() { let context: JSContex ...

  4. Cucumber Vs RobotFramework

    I asked this same question a little over a year ago on this list when we were at your stage. Before ...

  5. linux 内核源码arch/ 目录的前世今生

    历史的痕迹:在最新的linux-2.6.31/arch/arm/文件夹下,仍然保留Linux最初向ARM处理器移植的痕迹,最初的移植由黑客完成,在老的移植的代码文件的头部保留着黑客的名字:最初的ARM ...

  6. CSV文件导出2

    public void exportCSVFile( HttpServletResponse response, ResultSet rs,String fileName,String headers ...

  7. 利用jquery实现向左滚动效果及offset的使用

    昨天和今天做了一个轮播图,它的tab标签不是1,2,3这样的数据表示,而是使用圆圈表示,效果如下:

  8. Codeforces 375 D Tree and Queries

    Discription You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. ...

  9. Android SDK Manager 更新时的“https://dl-ssl.google.com refused”错误

    Android SDK Manager 消除SDK更新时的“https://dl-ssl.google.com refused”错误 消除SDK更新时,有可能会出现这样的错误:Download int ...

  10. python远程访问hive

    #!/usr/bin/pythonimport syssys.path.append('/home/zhoujie/Downloads/hive-0.7.0-cdh3u0/lib/py')from h ...