李洪强iOS开发之UICollectionView的使用
想做如下的界面效果(纯代码)





------------------------------------------------------------------------------------


----------------------------------------------------------------------------










//
// ViewController.m
// A22 - 李洪强CollectionVIew的使用
//
// Created by vic fan on 16/7/4.
// Copyright © 2016年 李洪强. All rights reserved.
//
#define SCREENW [UIScreen mainScreen].bounds.size.width
#define SCREENH [UIScreen mainScreen].bounds.size.height
#import "ViewController.h"
#import "LHQHeadCellView.h"
#import "LHQPromptBuyCell.h"
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>{
}
/**
* 父页面
*/
@property(nonatomic,strong)UICollectionView *collectionView;
@end
static NSString *const bannerId = @"bannerId";//轮播cell
static NSString *const PromptBuyId = @"PromptBuyId";//直接购买
static NSString *const fastBuyId = @"fastBuyId";//快速购买
static NSString *const dataId = @"dataID";//
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setUpUI];
}
/**
* 创建UI
*/
- (void)setUpUI{
/**
* 1 创建布局对象
*/
UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc]init];
// 2 设置横向滚动
flowlayout.scrollDirection = UICollectionViewScrollDirectionVertical;
/**
* 3 初始化collectionVIew 并且设置flowlayout
*/
_collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) collectionViewLayout:flowlayout];
/**
* 4 设置_collectionView的背景颜色
*/
_collectionView.backgroundColor = [UIColor grayColor];
/**
* 5 设置collectionVIew的代理和数据源的方法
*/
_collectionView.delegate = self;
_collectionView.dataSource = self;
/**
* 6 添加到主页面
*/
[self.view addSubview:_collectionView];
/**
* 7 collectionViewcell的注册
*/
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:bannerId];
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:PromptBuyId];
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:fastBuyId];
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:dataId];
//头视图和尾部视图的注册
[_collectionView registerClass:[LHQHeadCellView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeadView"];
[_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView"];
}
//----------------------------------------------------
#pragma mark collectionView代理方法
/**
* 代理方法1 返回几组cell
*
*/
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 4;
}
/**
* 代理方法2 每组有几个cell
*
*/
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
switch (section) {
case 0:{
return 1;
}
break;
case 1:{
return 2;
}
break;
case 2:{
return 4;
}
break;
case 3:{
return 3;
}
break;
default:{
return 0;
}
break;
}
}
/**
* 代理方法3 每个item的大小
*/
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
switch (indexPath.section) {
case 0:{
return CGSizeMake(SCREENW - 10, 150);
}
break;
case 1:{
return CGSizeMake(SCREENW/2 - 40, 150);
}
break;
case 2:{
return CGSizeMake(SCREENW / 2 - 10, 100);
}
break;
case 3:{
return CGSizeMake(SCREENW / 3 - 10, 100);
}
break;
default:{
return CGSizeMake(0, 0);
}
break;
}
}
/**
* 代理方法4 cell显示的内容
*
*/
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 1) {
LHQPromptBuyCell *cell = (LHQPromptBuyCell *)[collectionView dequeueReusableCellWithReuseIdentifier:PromptBuyId forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:dataId forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
/**
* 代理方法5 补充元素的视图
快速购买
*/
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
/**
* 5.1先初始化reusableView
*/
UICollectionReusableView *reusableView = nil;
/**
* 5.2 判断如果是头部视图
*/
if(kind == UICollectionElementKindSectionHeader){
/**
* 拿到第二组和第三组
*/
if(indexPath.section == 2|| indexPath.section == 3){
LHQHeadCellView *headCellV = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeadView" forIndexPath:indexPath ];
reusableView = headCellV;
}
/**
* 5.3 是尾部视图
*
*/
}else if (kind == UICollectionElementKindSectionFooter){
reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];
}
return reusableView;
}
/**
* 代理方法6 每个item的间距
*/
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
return 10;
}
/**
* 代理方法7 设置headView的大小
*/
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
if(section == 2 || section == 3){
return CGSizeMake(SCREENW , 25);
}
return CGSizeMake(SCREENW, 0);
}
/**
* 代理方法8 设置foot的大小
*/
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
return CGSizeMake(self.view.frame.size.width, 10);
}
/**
* 代理方法9 定义每个UICollectionView 的 margin
*/
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
if (section == 1) {
return UIEdgeInsetsMake(0, 20, 0, 20);
}else{
return UIEdgeInsetsMake(0, 5, 0, 5);
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
李洪强iOS开发之UICollectionView的使用的更多相关文章
- 李洪强iOS开发之Block和协议
李洪强iOS开发之Block和协议 OC语言BLOCK和协议 一.BOLCK (一)简介 BLOCK是什么?苹果推荐的类型,效率高,在运行中保存代码.用来封装和保存代码,有点像函数,BLOCK可以在任 ...
- 李洪强iOS开发之iOS社区收集
李洪强iOS开发之iOS社区收集 项目 简述 github 全球最大的代码仓库,无论是iOS开发还是Android开发没有人不知道这个网站,它也是一个社区,你可以去follow(关注)某些人或公司. ...
- 李洪强iOS开发之iOS工具收集
李洪强iOS开发之iOS工具收集 项目 简述 日期 我是怎么慢慢变懒的 : Jenkins + 蒲公英 使用Jenkins + 蒲公英使得项目打包给测试人员自动化,大大节省了劳动力 2015.04.1 ...
- 李洪强iOS开发之iOS学习方法收集
李洪强iOS开发之iOS学习方法收集 在这里收集一些iOS学习方法,会不断更新 项目 简述 日期 一年多iOS开发总结 作者总结了自己一年多的iOS学习经验,对于iOS初学者来说很多地方是可以借鉴的 ...
- 李洪强iOS开发之iOS好文章收集
李洪强iOS开发之iOS好文章收集 该文收集朋友们转发或自己的写的技术文章,如果你也有相关的好文章,欢迎留言,当好文章多的时候,我会对这些好文章进行分门别类 文章 简述 日期 直播服务配置 使用 ng ...
- 李洪强IOS开发之iOS好项目收集
李洪强IOS开发之iOS好项目收集 在这里收集一些最近出现的比较实用好玩的框架或者项目,会不断更新 项目 简述 日期 SCTableViewCell 类似与QQ侧滑删除Cell的Demo 201501 ...
- 李洪强iOS开发之iOS技术博客
李洪强iOS开发之iOS技术博客 注意:访问博客请直接点击博客,不要点击后面的RSS地址 博客地址 RSS地址 南峰子的技术博客 剑尖博客 图拉鼎 Henry Lee Dev Talk ...
- 李洪强iOS开发之带placeHolder的Textview
李洪强iOS开发之带placeHolder的Textview 01 - 创建工过程,定义全局属性,遵守textview的代理协议 02 - 添加一个textview和一个label 03 - 实现 ...
- 李洪强iOS开发之RunLoop的原理和核心机制
李洪强iOS开发之RunLoop的原理和核心机制 搞iOS之后一直没有深入研究过RunLoop,非常的惭愧.刚好前一阵子负责性能优化项目,需要利用RunLoop做性能优化和性能检测,趁着这个机会深入研 ...
随机推荐
- debain上安装mono3.4.0和jexus5.5.2
今天折腾了四个小时才把这个正确安装上,特此记录下.特别感谢群友的支持. 在VMware上新安装了Debain7.5,具体细节不复述了. 一.更新系统 #apt-get update #apt-get ...
- C++string的使用
在这里总结一下string的用法 String是可变长字符串,使用的时候要包含string头文件. 要想使用标准C++中string类,必须要包含 #include <string>// ...
- 双系统下利用MbrFix.exe卸载LINUX系统
前言: 不少同学笔记本都装的有双系统,一般都是LIUNX和WINDOWS的两个系统(由于以前对电脑各种无知)装了双系统,再次,小编就不在阐述双系统地各种不便,再次就强调一下,假若要卸载LINUX的话 ...
- P1179: [Apio2009]Atm
缩点+spfa最短路,因为最终不可能有环,所以直接spfa. ; type node=record f,t:longint; end; var n,m,s,i,j,ans,cnt,num,u,x,dg ...
- [shell基础]——uniq命令
uniq命令常见选项 去除重复行 -u 显示不重复的行 -d 显示有重复的行 -c 打印每一行重复的次数 测试文本内容如下: # cat 4.txt 11 ...
- C6011 正在取消对 null 指针的引用
- vim替换及多行注释命令
1.多行注释: . 进入命令行模式,按ctrl + v进入 visual block模式,然后按j, 或者k选中多行,把需要注释的行标记起来 . 按大写字母I,再插入注释符,例如// . 按esc键就 ...
- Liferay IDE3.1 M1的一些新功能
定于11月发布的Liferay IDE提供了一些让人期许的功能 1. code upgrade tools 这个工具将会帮助你把liferay 6.2的项目升级为7.0的项目.下面列举其主要功能 1. ...
- 【Same Tree】cpp
题目: Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...
- NYOJ-85 有趣的数 AC 分类: NYOJ 2014-01-17 21:42 240人阅读 评论(0) 收藏
这道题目就是,找规律,小学奥数,找规律不难吧, #include<stdio.h> int sc(int x); int main(){ int n=0; int num,cs,k; sc ...