UICollectionViewController xcode6.1 自定义Cell
2024-09-04 18:48:56
本文转载至 http://blog.csdn.net/daleiwang/article/details/40423219
虽然这个早已不是新东西了,但是之前项目中一直没有机会用,只知道跟tableView原理相同。
弄了个自定义UICollectionViewCell的小DEMO:
(1)在storyboard中拖拽一个UICollectionViewController:
(2)新建RootCollectionViewController继承自UICollectionViewController
- #import "RootCollectionViewController.h"
- #import "RootCollectionViewCell.h"
- @interface RootCollectionViewController ()
- @end
- @implementation RootCollectionViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.clearsSelectionOnViewWillAppear = NO;
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- }
- #pragma mark <UICollectionViewDataSource>
- - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
- return 1;
- }
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
- return 20;
- }
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
- static NSString * CellIdentifier = @"GradientCell";
- RootCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
- cell.backgroundColor = [UIColor colorWithRed:((110 * indexPath.row) / 255.0) green:((220 * indexPath.row)/255.0) blue:((330 * indexPath.row)/255.0) alpha:1.0f];
- [cell.lab setText:@"xxxx"];
- [cell.frontView setBackgroundColor:[UIColor redColor]];
- return cell;
- }
- #pragma mark <UICollectionViewDelegate>
- @end
(3)新建RootCollectionViewCell继承自UICollectionViewCell,头文件如下,自定义两个控件
- #import <UIKit/UIKit.h>
- @interface RootCollectionViewCell : UICollectionViewCell
- @property(nonatomic,weak)IBOutlet UILabel *lab;
- @property(nonatomic,weak)IBOutlet UIView *frontView;
- @end
(4)storyboard操作:
点击CollectionViewController,设置Class:
点击CollectionCell设置Class:
拖动一个View和lab到CollectionView上,设置约束为固定宽高上下居中:
点击左侧的CollectionCell,,将IBOutlet,连接到lab和frontView上:
设置大小及边距,当然了,xcode6以后,也可以根据不同的屏幕设置相应SizeClass下的边距:
(5)运行效果:
最新文章
- marathon新建应用映射端口限制
- (C#) 求两个数组的交集
- 如何定义DATATABLE,同时赋值
- linux网络流量实时监控工具之iptraf
- lightning mdb 源代码分析系列(3)
- Codeforces Round #310 (Div. 1) A. Case of Matryoshkas 水题
- bzoj2788
- HDU 1907
- IOS下双击背景, touchmove, 阻止页面背景scroll.
- jmeter实例演示
- 基于visual Studio2013解决C语言竞赛题之1010计算
- Nginx安装、平滑升级与虚拟机配置
- js中如何向json数组添加元素
- sublime No packages available for installation
- P4070 [SDOI2016]生成魔咒
- Vue中的render函数随笔
- C++.sprintf
- 记录初学Spring boot中使用GraphQL编写API的几种方式
- 安全的API接口解决方案
- [转]bing壁纸天天换 初识shell魅力
热门文章
- x86 idt
- 【UML】9种图+包图
- Luogu【P2065】贪心的果农(DP)
- 解决 sqlalchemy 报错:(1193, ";Unknown system variable &#39;tx_isolation&#39;";)
- 【2018.11.22】CTSC2018(模拟赛!)
- bzoj [Scoi2016]美味
- POJ2560 Freckles
- gdbt原理解析
- 关于EOF,转自新浪微博
- Netty构建游戏服务器(二)--Hello World