UICollectionViewController 用来创建流水布局 和 常见九宫格布局
2015-10-21 20:16
411 查看
1、概述
UICollectionView控件主要是用来做九宫格的,其用法与UITableView一样,首先要使控制器遵守数据源协议,再将控制器设置为UICollectionView的数据源。同样,控制器遵守了UICollectionView的代理后也可以实现代理方法等。
注意*在控制器push的时候,需要给控制器一个布局方式UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
需要添加<UICollectionViewDelegateFlowLayout>代理方法
使用手敲创建cell的时候,注意要在初始化定义cell的地方,换成自己的cell
[self.collectionView registerClass:[CollectionViewCell class]forCellWithReuseIdentifier:cellid];
// 与UITableView 不同的地方,每个cell 是 indexPath.item.
2、常用的数据源方法
注意**cell创建的时候需要在初始化的时候定义cell的id。在viewDidLoad中设置cell的ID
---------------------------------------------------------------------------------------------------神奇的分割线---------------------------------------------------------------------------------------------
1、概述
UICollectionView控件主要是用来做九宫格的,类似于android中的GridView控件。其用法与UITableView一样,首先要使控制器遵守数据源协议,再将控制器设置为UICollectionView的数据源。同样,控制器遵守了UICollectionView的代理后也可以实现代理方法等。
2、常用的数据源方法
设置UICollectionViewController一共有多少组:
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView
*)collectionView;
设置每组有多少单元格:
- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section;
设置每个单元格显示的内容:
- (UICollectionViewCell *)collectionView:(UICollectionView
*)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
3、常用的代理方法
设置每个单元格点击事件:
- (void)collectionView:(UICollectionView *)collectionView
didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
MJProduct *p = self.products[indexPath.item];
NSLog(@"点击了---%@", p.title);
}
4、UICollectionViewController必须调用的方法
(1)注册cell(告诉collectionView将来创建怎样的cell)
[self.collectionView registerClass:[UICollectionViewCell class]
forCellWithReuseIdentifier:@"product"];
在调用- (UICollectionViewCell *)collectionView:(UICollectionView
*)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;设置每个单元格显示的内容(UICollectionViewCell)之前必须注册cell,一般在viewDidLoad中调用上面方法注册cell。
例如:
- (void)viewDidLoad
{
[super viewDidLoad];
// 1.注册cell(告诉collectionView将来创建怎样的cell)
UINib *nib = [UINib nibWithNibName:@"MJProductCell" bundle:nil];
[self.collectionView registerNib:nib forCellWithReuseIdentifier:
MJProductCellID];//通过xid自定义的cell
/*
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:MJProductCellID];//使用默认的UICollectionViewCell
*/
// 2.设置collectionView的背景色
self.collectionView.backgroundColor = [UIColor whiteColor];
}
(2)从缓存池中取出cell
- (UICollectionViewCell *)collectionView:(UICollectionView
*)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell =
[collectionView dequeueReusableCellWithReuseIdentifier:@"product" forIndexPath:indexPath];
return cell;
}
在从缓存池取UICollectionViewCell重用时,不用再判断缓存池是否为空了,它与UITableView不同的是UICollectionView在缓存池没有找到cell时会自动创建新的cell。
(3)重写init方法,创建布局参数
- (id)init
{
// 1.流水布局
UICollectionViewFlowLayout *layout =
[[UICollectionViewFlowLayout alloc] init];
// 2.每个cell的尺寸
layout.itemSize = CGSizeMake(80, 80);
// 3.设置cell之间的水平间距
layout.minimumInteritemSpacing = 0;
// 4.设置cell之间的垂直间距
layout.minimumLineSpacing = 10;
// 5.设置所有cell组成的一个整体与屏幕(ViewController)四周距离
layout.sectionInset =
UIEdgeInsetsMake(layout.minimumLineSpacing, 0, 0, 0);
return [super initWithCollectionViewLayout:layout];
}
如果不创建布局参数程序会报错。一般在重写控制器的init方法中创建。
5、UICollectionViewFlowLayout
UICollectionViewFlowLayout称为”流水布局”, 用来约束cell的显示。
常见属性:
Cell的尺寸:
@property (nonatomic) CGSize itemSize;
cell之间的水平间距:
@property (nonatomic) CGFloat minimumInteritemSpacing;
cell之间的垂直间距:
@property (nonatomic) CGFloat minimumLineSpacing;
四周的内边距:
@property (nonatomic) UIEdgeInsets sectionInset;
原网址:http://my.oschina.net/u/2448717/blog/499771
UICollectionView控件主要是用来做九宫格的,其用法与UITableView一样,首先要使控制器遵守数据源协议,再将控制器设置为UICollectionView的数据源。同样,控制器遵守了UICollectionView的代理后也可以实现代理方法等。
注意*在控制器push的时候,需要给控制器一个布局方式UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
需要添加<UICollectionViewDelegateFlowLayout>代理方法
使用手敲创建cell的时候,注意要在初始化定义cell的地方,换成自己的cell
[self.collectionView registerClass:[CollectionViewCell class]forCellWithReuseIdentifier:cellid];
// 与UITableView 不同的地方,每个cell 是 indexPath.item.
2、常用的数据源方法
注意**cell创建的时候需要在初始化的时候定义cell的id。在viewDidLoad中设置cell的ID
---------------------------------------------------------------------------------------------------神奇的分割线---------------------------------------------------------------------------------------------
1、概述
UICollectionView控件主要是用来做九宫格的,类似于android中的GridView控件。其用法与UITableView一样,首先要使控制器遵守数据源协议,再将控制器设置为UICollectionView的数据源。同样,控制器遵守了UICollectionView的代理后也可以实现代理方法等。
2、常用的数据源方法
设置UICollectionViewController一共有多少组:
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView
*)collectionView;
设置每组有多少单元格:
- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section;
设置每个单元格显示的内容:
- (UICollectionViewCell *)collectionView:(UICollectionView
*)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
3、常用的代理方法
设置每个单元格点击事件:
- (void)collectionView:(UICollectionView *)collectionView
didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
MJProduct *p = self.products[indexPath.item];
NSLog(@"点击了---%@", p.title);
}
4、UICollectionViewController必须调用的方法
(1)注册cell(告诉collectionView将来创建怎样的cell)
[self.collectionView registerClass:[UICollectionViewCell class]
forCellWithReuseIdentifier:@"product"];
在调用- (UICollectionViewCell *)collectionView:(UICollectionView
*)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;设置每个单元格显示的内容(UICollectionViewCell)之前必须注册cell,一般在viewDidLoad中调用上面方法注册cell。
例如:
- (void)viewDidLoad
{
[super viewDidLoad];
// 1.注册cell(告诉collectionView将来创建怎样的cell)
UINib *nib = [UINib nibWithNibName:@"MJProductCell" bundle:nil];
[self.collectionView registerNib:nib forCellWithReuseIdentifier:
MJProductCellID];//通过xid自定义的cell
/*
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:MJProductCellID];//使用默认的UICollectionViewCell
*/
// 2.设置collectionView的背景色
self.collectionView.backgroundColor = [UIColor whiteColor];
}
(2)从缓存池中取出cell
- (UICollectionViewCell *)collectionView:(UICollectionView
*)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell =
[collectionView dequeueReusableCellWithReuseIdentifier:@"product" forIndexPath:indexPath];
return cell;
}
在从缓存池取UICollectionViewCell重用时,不用再判断缓存池是否为空了,它与UITableView不同的是UICollectionView在缓存池没有找到cell时会自动创建新的cell。
(3)重写init方法,创建布局参数
- (id)init
{
// 1.流水布局
UICollectionViewFlowLayout *layout =
[[UICollectionViewFlowLayout alloc] init];
// 2.每个cell的尺寸
layout.itemSize = CGSizeMake(80, 80);
// 3.设置cell之间的水平间距
layout.minimumInteritemSpacing = 0;
// 4.设置cell之间的垂直间距
layout.minimumLineSpacing = 10;
// 5.设置所有cell组成的一个整体与屏幕(ViewController)四周距离
layout.sectionInset =
UIEdgeInsetsMake(layout.minimumLineSpacing, 0, 0, 0);
return [super initWithCollectionViewLayout:layout];
}
如果不创建布局参数程序会报错。一般在重写控制器的init方法中创建。
5、UICollectionViewFlowLayout
UICollectionViewFlowLayout称为”流水布局”, 用来约束cell的显示。
常见属性:
Cell的尺寸:
@property (nonatomic) CGSize itemSize;
cell之间的水平间距:
@property (nonatomic) CGFloat minimumInteritemSpacing;
cell之间的垂直间距:
@property (nonatomic) CGFloat minimumLineSpacing;
四周的内边距:
@property (nonatomic) UIEdgeInsets sectionInset;
原网址:http://my.oschina.net/u/2448717/blog/499771
相关文章推荐
- MiniUI动态添加table表格
- IOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem
- [android]AlertDialog和AlertDialog.Builder
- easyui 调用ajax时的loading效果
- Easyui动态加载后台数据的例子
- ConcurrentLinkedQueue 使用
- UVALive-4287 Proving Equivalences (有向图的强连通分量)
- 【PAT】1101. Quick Sort (25)
- iphone利用UIPopoverPresentationController实现popover
- Maximum Subsequence Sum
- Hadoop Sequencefile using Hadoop 2 Apis
- [LeetCode-225] Implement Stack using Queues(两个队列实现栈)
- 更改Xcode Build的位置
- surfaceView和View最本质的区别
- 实现一个对象验证库系列 -- 3) Fluent以及扩展方法实现 (请大神批评)
- GUID字符串做主键与Number类型做主键 的执行效率比较
- 关于hibernate的could not execute query错误
- NGUI图集字体
- 使用UITabBarViewCOntroller时隐藏tabbar
- ios代码编写UIButton,设置图片,图片不显示