iOS开发:集合视图 UICollectionView
2015-09-26 14:29
543 查看
iOS:集合视图 UICollectionView
1.创建集合视图的步骤:
(1).使用系统的布局UICollectionViewFlowLayout
//初始化标准瀑布流的对象
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
//每一块的大小
flowLayout.itemSize = CGSizeMake(355, 655);
//最小行间距
flowLayout.minimumLineSpacing = 15;
//最小列间距
flowLayout.minimumInteritemSpacing = 15;
//设置上左下右的距离
flowLayout.sectionInset = UIEdgeInsetsMake(20, 15, 20, 15); //整个collectionView距离上左下右
//滚动方向(默认是纵向vertical)
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
(2).设置代理,设置数据源
<UICollectionViewDataSource, UICollectionViewDelegate>
//初始化CollectionView
_collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) collectionViewLayout:flowLayout];
_collectionView.dataSource = self;
_collectionView.delegate = self;
//整页滚动
_collectionView.pagingEnabled = YES;
[self.view addSubview:_collectionView];
(3).设置自定义cell
#warning 重点:collectionView 注册cell
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"identifier"];
//实现协议方法:
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return _dataSourceArray.count; //返回块数
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
//直接进重用池找可用cell
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
cell.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 225.0 green:arc4random() % 256 / 225.0 blue:arc4random()
% 256 / 225.0 alpha:1];
return cell;
}
//获取当前cell的下标
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath{
NSLog(@"%ld", indexPath.row);
}
(4).设置 <UICollectionViewDelegateFlowLayout>代理
@protocol UICollectionViewDelegateFlowLayout <UICollectionViewDelegate>
@optional
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath
*)indexPath;
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
insetForSectionAtIndex:(NSInteger)section;
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;
@end
1.创建集合视图的步骤:
(1).使用系统的布局UICollectionViewFlowLayout
//初始化标准瀑布流的对象
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
//每一块的大小
flowLayout.itemSize = CGSizeMake(355, 655);
//最小行间距
flowLayout.minimumLineSpacing = 15;
//最小列间距
flowLayout.minimumInteritemSpacing = 15;
//设置上左下右的距离
flowLayout.sectionInset = UIEdgeInsetsMake(20, 15, 20, 15); //整个collectionView距离上左下右
//滚动方向(默认是纵向vertical)
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
(2).设置代理,设置数据源
<UICollectionViewDataSource, UICollectionViewDelegate>
//初始化CollectionView
_collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) collectionViewLayout:flowLayout];
_collectionView.dataSource = self;
_collectionView.delegate = self;
//整页滚动
_collectionView.pagingEnabled = YES;
[self.view addSubview:_collectionView];
(3).设置自定义cell
#warning 重点:collectionView 注册cell
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"identifier"];
//实现协议方法:
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return _dataSourceArray.count; //返回块数
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
//直接进重用池找可用cell
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
cell.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 225.0 green:arc4random() % 256 / 225.0 blue:arc4random()
% 256 / 225.0 alpha:1];
return cell;
}
//获取当前cell的下标
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath{
NSLog(@"%ld", indexPath.row);
}
(4).设置 <UICollectionViewDelegateFlowLayout>代理
@protocol UICollectionViewDelegateFlowLayout <UICollectionViewDelegate>
@optional
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath
*)indexPath;
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
insetForSectionAtIndex:(NSInteger)section;
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;
@end
相关文章推荐
- 利用break和continue窥探空格输入与回车输入
- Android通用UI开发技术(1)
- General Problem Solving Techniques [Examples]~C
- 第一行代码笔记4(UI)
- web开发性能优化---UI接口章
- SpriteBuilder切换解决方案以及CCB的修改与保存
- SpriteBuilder切换解决方案以及CCB的修改与保存
- SpriteBuilder切换解决方案以及CCB的修改与保存
- Java 根据 HashMap 的 value 进行排序
- iOS开发UI系列之屏幕适配
- iOS开发UI系列之UIButton的基本使用
- iOS开发UI系列之UIImageView的基本使用
- iOS开发UI系列之UILabel的基本使用
- 【APUE】Chapter9 Process Relationships
- iOS开发UI系列之UIScrollView
- iOS开发UI系列之图片拉伸
- UNIX环境高级编程学习环境搭建---apue.h(第三版)
- QtQuick桌面应用程序开发指导 3)达到UI而功能_B 4)动态管理Note物_A
- POJ - 2533 Longest Ordered Subsequence(最长上升子序列)
- Android UI之ListView+CheckBox(避免抢占焦点)