UICollectionViewCell中cell的定义与自定义方式总结
来源:互联网 发布:淘宝口令劫持举报 编辑:程序博客网 时间:2024/05/18 14:13
1 、使用UICollectionViewCell
.h文件 #import <UIKit/UIKit.h>@interface ViewController01 : UIViewController@end.m文件#import "ViewController01.h"@interface ViewController01 ()<UICollectionViewDataSource, UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>@end@implementation ViewController01- (void)viewDidLoad { [super viewDidLoad]; //集合视图 //UICollectionViewFlowLayoutUICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; //flowLayout属性 自定义flowLayout //1.cell的大小 flowLayout.itemSize = CGSizeMake(50,20); //2.行与行之间的最小间距 flowLayout.minimumLineSpacing = 50; //3.同一行之间每个item之间的最小间距 flowLayout.minimumInteritemSpacing = 30; //4.滚动方向(垂直和水平) flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; //创建collectionView UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout]; collectionView.delegate = self; collectionView.dataSource = self; //注册 [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellID"]; [self.view addSubview:collectionView];}#pragma mark - UICollectionView dataSource//每组单元格(items)的个数- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 50;}//具体的单元格- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath]; cell.backgroundColor = [UIColor redColor]; UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)]; view1.backgroundColor = [UIColor orangeColor]; //选中的背景视图 cell.selectedBackgroundView = view1; return cell; }#pragma mark - UICollectionViewDelegateFlowLayout//每个单元格的size尺寸-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ CGFloat width = arc4random()%10*10+5;CGFloat height = arc4random()%10*10+5; return CGSizeMake(width, height);}@end
2 自定义CollectionViewCell(纯代码 xib)
a)注册(TopCell Class) 重用
首先创建一个UICollectionViewCell .h文件 #import <UIKit/UIKit.h>@interface CollectionViewCell02 : UICollectionViewCell { UIImageView *imageView;}@end.m文件#import "CollectionViewCell02.h"@implementation CollectionViewCell02- (instancetype) initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self _createSubView]; } return self;}- (void) _createSubView { CGRect frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height); imageView = [[UIImageView alloc] initWithFrame:frame]; imageView.backgroundColor = [UIColor redColor]; imageView.center = self.contentView.center; [self.contentView addSubview:imageView];}@end在ViewController中.h文件#import <UIKit/UIKit.h>@interface ViewController02 : UIViewController@end.m文件#import "ViewController02.h"#import "CollectionViewCell02.h"@interface ViewController02 ()<UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource>@end@implementation ViewController02- (void)viewDidLoad { [super viewDidLoad]; UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; flowLayout.itemSize = CGSizeMake(60, 60); flowLayout.minimumLineSpacing = 30; flowLayout.minimumInteritemSpacing = 10; flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical; UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout]; collectionView.dataSource = self; collectionView.delegate = self; //注册 [collectionView registerClass:[CollectionViewCell02 class] forCellWithReuseIdentifier:@"cell"]; [self.view addSubview:collectionView];}#pragma mark - 代理方法的实现- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 50;}- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { CollectionViewCell02 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];// cell.backgroundColor = [UIColor redColor]; return cell;}@end
b)注册(Nib ) 重用
先创建一个UICollectionViewCell文件,同是创建它的Xib文件。这里在里面添加了一个imageView,并进行了关联。
接下来使用cell:
.h文件
#import <UIKit/UIKit.h>@interface ViewController03 : UIViewController@end.m文件#import "ViewController03.h"#import "CollectionViewCell03.h"@interface ViewController03 ()<UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource>@end@implementation ViewController03- (void)viewDidLoad { [super viewDidLoad]; //集合视图 //UICollectionViewFlowLayout UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; //flowLayout属性 自定义flowLayout //1.cell的大小 flowLayout.itemSize = CGSizeMake(120,80); //2.行与行之间的最小间距 flowLayout.minimumLineSpacing = 30; //3.同一行之间每个item之间的最小间距 flowLayout.minimumInteritemSpacing = 30; //4.滚动方向(垂直和水平) flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; //创建collectionView UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout]; collectionView.delegate = self; collectionView.dataSource = self;//nib注册方法 UINib *CollectionViewCell = [UINib nibWithNibName:@"CollectionViewCell03" bundle:nil]; //注册 [collectionView registerNib:CollectionViewCell forCellWithReuseIdentifier:@"cell"]; [self.view addSubview:collectionView];}//代理- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 50;}- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { CollectionViewCell03 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; return cell;}@end **3 使用StoryBoard 重用(Identifier)** 使用StoryBoard不需要注册,直接用就行 往StoryBoard中拖一个CollectionView,这个集合视图自带cell,为cell添加一个imageView,填充好图片,然后设好重用ID,如图:![这里写图片描述](http://img.blog.csdn.net/20160722211932976) 使用如下:.h文件: #import <UIKit/UIKit.h>@interface ViewController : UIViewController@end.m文件#import "ViewController.h"@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad];}#pragma mark - UICollectionView dataSource//每组单元格(items)的个数- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 50;}//具体的单元格- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; return cell;}@end
0 0
- UICollectionViewCell中cell的定义与自定义方式总结
- 自定义cell 3中方式
- 获取tableView(UICollectionViewCell)中cell相对于(UICollectionView)tableView.superView的区域坐标(位置)
- 自定义Cell总结(注册Cell)(registerNib: 与 registerClass: 的区别)
- IOS中UICollectionViewCell的问题
- 自定义UICollectionViewCell中控件为nil
- UITableViewCell重用问题,自定义与非自定义cell的两种处理方式
- 自定义cell的三种方式
- IOS 自定义Cell的3种方式
- 自定义加载cell 两种靠谱的方式
- 自定义cell的复用方式1
- 自定义cell的复用方式2
- Swift自定义UICollectionViewCell不显示的解决方案
- UICollectionView学习<2>之UICollectionViewCell的自定义
- 自定义UICollectionViewCell 以及碰到的问题
- UICollectionView和自定义UICollectionViewCell的基本用法
- UICollectionViewCell的四种创建方式
- 总结-通过代码自定义cell(cell的高度不一致)
- Pin - A Dynamic Binary Instrumentation Tool
- 路由套路由设置
- 实验案例3-3银行业务队列简单模拟:
- jsonp和xml格式生成与调用
- 让Django支持Sql Server作后端数据库
- UICollectionViewCell中cell的定义与自定义方式总结
- 最少找硬币问题
- VMware中RedHat下安装VMware Tools
- 卵形线
- java之TreeSet里添加自定义对象
- SpringMVC工作原理解读--DispatcherServlet类源码解读(初级)
- J2EE进阶(十)SSH框架整合常见问题汇总(一)
- OpenGL模拟太阳系运行
- 树莓派 RECALBOX 复合电视信号输出配置