本文转载至 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)运行效果:

最新文章

  1. marathon新建应用映射端口限制
  2. (C#) 求两个数组的交集
  3. 如何定义DATATABLE,同时赋值
  4. linux网络流量实时监控工具之iptraf
  5. lightning mdb 源代码分析系列(3)
  6. Codeforces Round #310 (Div. 1) A. Case of Matryoshkas 水题
  7. bzoj2788
  8. HDU 1907
  9. IOS下双击背景, touchmove, 阻止页面背景scroll.
  10. jmeter实例演示
  11. 基于visual Studio2013解决C语言竞赛题之1010计算
  12. Nginx安装、平滑升级与虚拟机配置
  13. js中如何向json数组添加元素
  14. sublime No packages available for installation
  15. P4070 [SDOI2016]生成魔咒
  16. Vue中的render函数随笔
  17. C++.sprintf
  18. 记录初学Spring boot中使用GraphQL编写API的几种方式
  19. 安全的API接口解决方案
  20. [转]bing壁纸天天换 初识shell魅力

热门文章

  1. x86 idt
  2. 【UML】9种图+包图
  3. Luogu【P2065】贪心的果农(DP)
  4. 解决 sqlalchemy 报错:(1193, &quot;Unknown system variable &#39;tx_isolation&#39;&quot;)
  5. 【2018.11.22】CTSC2018(模拟赛!)
  6. bzoj [Scoi2016]美味
  7. POJ2560 Freckles
  8. gdbt原理解析
  9. 关于EOF,转自新浪微博
  10. Netty构建游戏服务器(二)--Hello World