UICollectionView 和 UICollectionViewController 类是iOS6 新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableView 和 UITableViewController 类

与UICollectionView有关的三个协议:UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout

几个常用到得方法(需遵守协议)

- (void)viewDidLoad
{
[super viewDidLoad];
//创建一个布局方式
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
// layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
layout.headerReferenceSize = CGSizeMake(, );//header的size
layout.footerReferenceSize = CGSizeMake(, );
layout.sectionInset = UIEdgeInsetsMake(, , , );
//创建一个集合视图
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
collectionView.dataSource = self;
collectionView.delegate = self;
[collectionView registerClass:[CustomCell class] forCellWithReuseIdentifier:@"cell"];
//注册header
[collectionView registerClass:[CustomHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
//注册footer 增补视图
[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"]; [self.view addSubview:collectionView];
[layout release];
[collectionView release];
// Do any additional setup after loading the view.
}
//定义展示的Section的个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return ;
}
//定义展示的UICollectionViewCell的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return ;
} //每个UICollectionViewCell展示的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"cell";
CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
cell.titleLabel.text = [NSString stringWithFormat:@"%d",indexPath.row];
cell.backgroundColor = [UIColor orangeColor];
return cell;
}
//定义每个UICollectionView 中cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(, );
}
//增补视图header与footer
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
if (kind == UICollectionElementKindSectionHeader) {
CustomHeaderView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
view.backgroundColor = [UIColor greenColor];
return view;
}else
{
UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
view.backgroundColor = [UIColor redColor];
return view;
} }

对UICollectionView的学习的更多相关文章

  1. UICollectionView基础学习

    相信了解UICollectionView的也一定听过瀑布流吧,开始之前先提供两个瀑布流,有时间的可以深入研究研究 https://github.com/dingpuyu/WaterFall https ...

  2. swift系统学习控件篇:UITableView+UICollectionView

    工作之余,学习下swift大法.把自己的学习过程分享一下.当中的布局很乱,就表在意这些细节了.直接上代码: UITableView: // // ViewController.swift // UIt ...

  3. UICollectionView 具体解说学习

    UICollectionView 和UITableView非常像,是APPLE公司在iOS 6后推出的用于处理图片这类UITableView 布局困难的控件,和UITableView 一样,它也有自己 ...

  4. UICollectionLayout布局 —— UIKit之学习UICollectionView记录二《流水布局》

    重点知识 一. 加载collectionView注意事项 1.创建collectionView,有两种方式 :一种是xib和一种是纯代码:设置代理和数据源,注册cell,配置流水布局的属性,如上.下. ...

  5. iOS 学习 - 20 UICollectionView 移动 Item ,类似背包

    有100个 item,数据源只有20个,只能在 20 个之间移动,防止 item 复用,出现 bug 方法一:苹果自带 //UICollectionViewDataSource- (BOOL)coll ...

  6. 自定义UICollectionLayout布局 —— UIKit之学习UICollectionView记录一《瀑布流》

    一.思路 思路一:比较每一行所有列的cell的高度,从上到下(也就是从第一行开始),从最短的开始计算,(记录下b的高度和索引,从开始计算,依次类推) 思路二:设置上.下.左.右间距和行间距.列间距及列 ...

  7. UITableView和UICollectionView的方法学习一

    参考资料 UITableView UICollectionView UICollectionViewDataSource UICollectionViewDelegate UICollectionVi ...

  8. iOS学习路线图

    一.iOS学习路线图   二.iOS学习路线图--视频篇       阶 段 学完后目标 知识点 配套学习资源(笔记+源码+PPT) 密码 基础阶段 学习周期:24天       学习后目标:    ...

  9. iOS及Mac开源项目和学习资料【超级全面】

    UI 下拉刷新 EGOTableViewPullRefresh – 最早的下拉刷新控件. SVPullToRefresh – 下拉刷新控件. MJRefresh – 仅需一行代码就可以为UITable ...

随机推荐

  1. hadoop集群配置实例

    1)ssh配置 http://allthingshadoop.com/2010/04/20/hadoop-cluster-setup-ssh-key-authentication/ 2) 修改打开文件 ...

  2. Jquery判断div是否显示

    $("#test").is(":hidden");//是否隐藏 $("#test").is(":visible");// ...

  3. ubuntu 防火墙 添加策略 解决mysql远程访问问题

    ubuntu 的iptables 文件不在 init.d中 不能 service iptables restart 只修改 /etc/iptables 文件也不管用 sudo iptables -L ...

  4. [开发笔记]-jQuery获取checkbox选中项等操作及注意事项

    今天在做一个项目功能时需要显示checkbox选项来让用户进行选择,由于前端不是很熟练,所以做了一个简单的Demo,其中遇到一些小问题,特记录下来,希望能帮到遇到类似问题的同学们. 1. 获取chec ...

  5. poj1274 二分匹配

    今天复习二分匹配,A 了一道模板题. 二分匹配需要理解增广路的寻找.用dfs来更新最大匹配.注意一些点:赋初值:愚蠢地把==写成了= ; 然后match的记值;每个点都要重新走一遍. #include ...

  6. C++,1....n中随机等概率的输出m个不重复的数(假设n远大于m)。

    #include <stdlib.h> #include <time.h> knuth(int n, int m) { srand((unsigned )); ; i < ...

  7. Oracle创建用户并给用户授权查询指定表或视图的权限

    MSV31账户登录数据库进行如下操作: CREATE USER NORTHBOUND IDENTIFIED BY NORTHBOUND  DEFAULT TABLESPACE "TBS_DN ...

  8. PHP及Javascript 正则判断中文(转)

    UTF-8匹配: 在javascript中,要判断字符串是中文是很简单的.比如: var str = "php编程"; if (/^[\u4e00-\u9fa5]+$/.test( ...

  9. SharePoint 2013 Nintex Workflow 工作流帮助(一)

    博客地址 http://blog.csdn.net/foxdave 接下来一段时间的内容中,我们基于SharePoint 2013来了解一下Nintex Workflow的具体内容. 之前的几篇由于之 ...

  10. public protected default private

    简单来说,如果让一个变量或者方法,只想让自己类中的访问,那么就将它们设置成private 如果你想让一个变量或者方法,本包中的类可以访问,而且子类也可访问,但是包外的缺不想让他访问.就设置成prote ...