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. bzoj 2257: [Jsoi2009]瓶子和燃料

    #include<cstdio> #include<iostream> #include<algorithm> #include<cmath> usin ...

  2. 类成员函数作为pthread_create函数参数

    from:http://www.cnblogs.com/shijingxiang/articles/5389294.html 近日需要将线程池封装成C++类,类名为Threadpool.在类的成员函数 ...

  3. USB鼠标按键驱动

    现象:把USB设备接到PC 1. 右下角弹出"发现android phone" 2. 跳出一个对话框,提示你安装驱动程序 问1. 既然还没有"驱动程序",为何能 ...

  4. Mac OS X 卸载MySQL

    sudo rm /usr/local/mysqlsudo rm -rf /usr/local/mysql*sudo rm -rf /Library/StartupItems/MySQLCOMsudo ...

  5. sqlite3 SQL常用语句

    1. select SELECT LastName,FirstName FROM Persons; SELECT * FROM Persons; 2. where SELECT * FROM Pers ...

  6. Easy UI 遮罩(MASK)

    From :http://blog.csdn.net/luminji/article/details/16984839 Easy UI 的各类控件有些带了遮罩功能,如 DataGrid,可以这样使用: ...

  7. 经典线程同步 互斥量Mutex

    阅读本篇之前推荐阅读以下姊妹篇: <秒杀多线程第四篇一个经典的多线程同步问题> <秒杀多线程第五篇经典线程同步关键段CS> <秒杀多线程第六篇经典线程同步事件Event& ...

  8. [转]centos7 配置yum源(本地+光盘)

    from:http://wangyan.org/blog/setup-local-yum-repo.html 一,本地 1.创建本地yum仓库 1.mkdir -p /yum/local #可以有N级 ...

  9. [转] Linux内核代码风格 CodingStyle [CH]

    from:http://blog.csdn.net/jiang_dlut/article/details/8163731 中文版维护者: 张乐 Zhang Le <r0bertz@gentoo. ...

  10. 使用WebClient Post方式模拟上传文件和数据

    假如某网站有个表单,例如(url: http://localhost/login.aspx):帐号  密码 我们需要在程序中提交数据到这个表单,对于这种表单,我们可以使用 WebClient.Uplo ...