UICollectlionView继承自UIScrollerview,跟tableview的使用很相似。

下面是UIcollectionView的一些属性和代理方法。

#import "ViewController.h"
#import "GoodsCollectionViewCell.h" @interface ViewController ()
@property(nonatomic,strong)NSMutableArray *dataArray;
@end @implementation ViewController - (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor lightGrayColor];
_dataArray = [NSMutableArray array];
for (int i = ; i < ; i ++) {
[_dataArray addObject:[NSString stringWithFormat:@"%d",i]];
} //确定是水平滚动,还是垂直滚动
UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical; self.collectionView=[[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout];
self.collectionView.dataSource=self;
self.collectionView.delegate=self;
[self.collectionView setBackgroundColor:[UIColor clearColor]]; //注册Cell,必须要有
[self.collectionView registerNib:[UINib nibWithNibName:@"GoodsCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"GoodsCollectionViewCell"]; [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
[self.view addSubview:self.collectionView]; } #pragma mark -- UICollectionViewDataSource //定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return ;
} //定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return ;
} //每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
GoodsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GoodsCollectionViewCell" forIndexPath:indexPath];
cell.goodsImageView.image = [UIImage imageNamed:_dataArray[indexPath.row]];
cell.backgroundColor = [UIColor whiteColor];
return cell;
} - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *headReusableView;
//此处是headerView
if (kind == UICollectionElementKindSectionHeader) {
headReusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
//防止复用时候重复添加title
[headReusableView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; if (indexPath.section == ) {
UIView *headerADView = [[ UIView alloc]initWithFrame:CGRectMake(, , self.view.frame.size. width, )];
headerADView.backgroundColor = [UIColor yellowColor];
UILabel *titleADLabel = [[UILabel alloc]initWithFrame:CGRectMake(, , self.view.frame.size. width - , )];
titleADLabel.text = @"这个是广告位"; UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(, , self.view.frame.size. width - , )];
titleLabel.text = @"推荐商品";
headReusableView.backgroundColor = [UIColor colorWithWhite:. alpha:]; [headerADView addSubview:titleADLabel];
[headReusableView addSubview:headerADView];
[headReusableView addSubview:titleLabel];
}else{ UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(, , self.view.frame.size. width - , )];
titleLabel.text = @"推荐商品";
headReusableView.backgroundColor = [UIColor colorWithWhite:. alpha:];
[headReusableView addSubview:titleLabel];
}
}
return headReusableView;
} //执行的 headerView 代理 返回 headerView 的高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
if (section == ) {
return CGSizeMake(, + ); }
return CGSizeMake(, );
} #pragma mark --UICollectionViewDelegateFlowLayout //定义每个Item 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(self.view.frame.size.width / 3.0, );
} //定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(, , , );
} #pragma mark --UICollectionViewDelegate //UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
GoodsCollectionViewCell * cell = (GoodsCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
//临时改变个颜色,看好,只是临时改变的。如果要永久改变,可以先改数据源,然后在cellForItemAtIndexPath中控制。(和UITableView差不多吧!O(∩_∩)O~)
cell.backgroundColor = [UIColor greenColor];
} //返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
} // 两个cell之间的最小间距,是由API自动计算的,只有当间距小于该值时,cell会进行换行
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return ;
} // 两行之间的最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return ;
} @end

注意:

1.定义collectionView的头文件的时候,需要先注册。

2.collectionView每一行显示几个cell是根据cell 的宽度自动显示的。如:scrollerview的宽度为320,cell宽度为 90, 3 * 90 = 270 < 320 , 4 * 90 = 360 > 320,这个时候就显示3个cell。

3.每一行cell的位置的微调可以通过代理方法进行调整。

iOS---UICollectlionView 的使用的更多相关文章

  1. iOS可视化动态绘制连通图

    上篇博客<iOS可视化动态绘制八种排序过程>可视化了一下一些排序的过程,本篇博客就来聊聊图的东西.在之前的博客中详细的讲过图的相关内容,比如<图的物理存储结构与深搜.广搜>.当 ...

  2. 【疯狂造轮子-iOS】JSON转Model系列之二

    [疯狂造轮子-iOS]JSON转Model系列之二 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇<[疯狂造轮子-iOS]JSON转Model系列之一> ...

  3. 【疯狂造轮子-iOS】JSON转Model系列之一

    [疯狂造轮子-iOS]JSON转Model系列之一 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 之前一直看别人的源码,虽然对自己提升比较大,但毕竟不是自己写的,很容易遗 ...

  4. iOS总结_UI层自我复习总结

    UI层复习笔记 在main文件中,UIApplicationMain函数一共做了三件事 根据第三个参数创建了一个应用程序对象 默认写nil,即创建的是UIApplication类型的对象,此对象看成是 ...

  5. iOS代码规范(OC和Swift)

    下面说下iOS的代码规范问题,如果大家觉得还不错,可以直接用到项目中,有不同意见 可以在下面讨论下. 相信很多人工作中最烦的就是代码不规范,命名不规范,曾经见过一个VC里有3个按钮被命名为button ...

  6. JS调用Android、Ios原生控件

    在上一篇博客中已经和大家聊了,关于JS与Android.Ios原生控件之间相互通信的详细代码实现,今天我们一起聊一下JS调用Android.Ios通信的相同点和不同点,以便帮助我们在进行混合式开发时, ...

  7. 告别被拒,如何提升iOS审核通过率(上篇)

    iOS审核一直是每款移动产品上架苹果商店时面对的一座大山,每次提审都像是一次漫长而又悲壮的旅行,经常被苹果拒之门外,无比煎熬.那么问题来了,我们有没有什么办法准确把握苹果审核准则,从而提升审核的通过率 ...

  8. Swift3.0服务端开发(一) 完整示例概述及Perfect环境搭建与配置(服务端+iOS端)

    本篇博客算是一个开头,接下来会持续更新使用Swift3.0开发服务端相关的博客.当然,我们使用目前使用Swift开发服务端较为成熟的框架Perfect来实现.Perfect框架是加拿大一个创业团队开发 ...

  9. Summary of Critical and Exploitable iOS Vulnerabilities in 2016

    Summary of Critical and Exploitable iOS Vulnerabilities in 2016 Author:Min (Spark) Zheng, Cererdlong ...

  10. 黑云压城城欲摧 - 2016年iOS公开可利用漏洞总结

    黑云压城城欲摧 - 2016年iOS公开可利用漏洞总结 作者:蒸米,耀刺,黑雪 @ Team OverSky 0x00 序 iOS的安全性远比大家的想象中脆弱,除了没有公开的漏洞以外,还有很多已经公开 ...

随机推荐

  1. RNA分析要点

    1. 有参与无参转录组分析 2. lncRNA分析 以RNA-Seq测序技术为基础的转录组测序作为高通量测序时代核心技术之一,已在生物科学及医学领域前沿研究中获得广泛应用.RNA-Seq可进行全基因组 ...

  2. 在使用html5的video标签播放视频时为何只有声音却没有图像

    在使用html5的video标签播放视频时为何只有声音却没有图像? 答:使用格式化工厂转个编码就行了,MP4有3种编码,mpg4(xdiv),,mpg4(xvid),avc(h264)转换成H264编 ...

  3. 2018.08.17 洛谷[POI2010]GRA-The Minima Game(线性dp)

    传送门 短代码神奇dp. 自己yy的思路居然1A了好高兴啊! 不难想到每个人选择的时候一定是取连续的最大的那一段数,自然需要先排序. 然后可以用dp[i]表示当前最大数是a[i]的时候先手可以获得的最 ...

  4. MATLAB实现截位的问题

    讨论MATLAB怎样提取10进制中的位的方法,因为做FFT时要用到截位,相去验证它,向同庆请教, 原来只是除以2的N次方,取模取余就行了,可恨我还想了一下午,也没有一个好办法. 接下来的问题是,对于负 ...

  5. Dbutils学习(介绍和入门)

    一:Dbutils是什么?(当我们很难理解一个东西的官方解释的时候,就让我们记住它的作用)      Dbutils:主要是封装了JDBC的代码,简化dao层的操作.      作用:帮助java程序 ...

  6. Easy CHM使用简明教程

    近日整理硬盘,发现下载有许多DOC.JPEG.HTML等 格式的学习资料,也包括一些电子书资料:而其中的DOC.HTML等资料在学习浏览时显得很不方便,不同格式的文件需要使用不同的打开方式.近而发现电 ...

  7. 重大发现 springmvc Controller 高级接收参数用法

    1.  数组接收 @RequestMapping(value="deleteRole.json") @ResponseBody public Object deleteRole(S ...

  8. ASYNC PROGRAMING IN JAVASCRIPT[转]

    本文从异步风格讲起,分析Javascript中异步变成的技巧.问题和解决方案.具体的,从回调造成的问题说起,并谈到了利用事件.Promise.Generator等技术来解决这些问题. 异步之殇 NON ...

  9. (二分匹配 模板)过山车 -- hdu --2063

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=2063 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  10. C语言 fread()与fwrite()函数说明与示例

    1.作用 读写文件数据块. 2.函数原型 (1)size_t fread ( void * ptr, size_t size, size_t count, FILE * stream ); 其中,pt ...