#pragma mark -- 创建CollectionView

- (void)createCollectionView{

//关闭自适应

self.automaticallyAdjustsScrollViewInsets = NO;

UICollectionViewFlowLayout *fl = [[UICollectionViewFlowLayout alloc]init];

_collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64-49) collectionViewLayout:fl];

_collectionView.dataSource = self;

_collectionView.delegate = self;

_collectionView.backgroundColor = [UIColor colorWithRed:237/255.0 green:237/255.0 blue:237/255.0 alpha:0.8 ];

[self.view addSubview: _collectionView];

//布局

fl.minimumInteritemSpacing = 0;

fl.minimumLineSpacing = 5;

//注册cell

[_collectionView registerNib:[UINib nibWithNibName:@"RootCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"JXCell"];

[_collectionView registerNib:[UINib nibWithNibName:@"JXTeSeCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"JXTSCell"];

//注册header

[_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"oneHeader"];

[_collectionView registerClass:[JXHeaderReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"JXHeader"];

#pragma mark -- 下拉刷新

MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

//下拉重新加载数据

self.page = 1;

[_dataArr removeAllObjects];

[self loadData];

}];

[header setTitle:@"下拉刷新" forState:MJRefreshStatePulling];

[header setTitle:@"正在刷新" forState:MJRefreshStateRefreshing];

_collectionView.header = header;

//上拉

MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{

//上拉加载更多

self.page += 1;

[self setMyUrl];

[self loadData];

}];

[footer setTitle:@"下拉刷新" forState:MJRefreshStatePulling];

[footer setTitle:@"正在刷新" forState:MJRefreshStateRefreshing];

_collectionView.footer = footer;

}

#pragma mark -- dataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

return 2;

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

if (section == 0) {

return 4;

}

return _dataArr.count;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

if (indexPath.section == 0) {

//4个特色专区

JXTeSeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"JXTSCell" forIndexPath:indexPath];

cell.backgroundColor = [UIColor whiteColor];

if (_teSeArr.count<=0) {

return cell;

}

//与滚动模型 共用

ScrollModel *model = _teSeArr[indexPath.item];

[cell loadDataFromModel:model];

return cell;

}else{

//精品推荐

RootCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"JXCell" forIndexPath:indexPath];

if (_dataArr.count <=0) {

return cell;

}

JXJingPinModel *model = _dataArr[indexPath.item];

[cell loadDataFromJXModel:model];

cell.backgroundColor = [UIColor whiteColor];

return cell;

}

}

//

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

#pragma mark -- 一组 滚动视图

if (indexPath.section == 0) {

UICollectionReusableView *headerView = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"oneHeader" forIndexPath:indexPath];

ToAdScrollview *scrollView = [[ToAdScrollview alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200) andImageArr:_scrollArr];

scrollView.backgroundColor = [UIColor whiteColor];

__weak typeof (self)weakSelf = self;

scrollView.block = ^(int index){

//实现点击某个视图跳转

RootWebViewController *webVC = [[RootWebViewController alloc]init];

webVC.hidesBottomBarWhenPushed = YES;

webVC.url = @"http://www.nanyibang.com/school/school.php?id=378";

[weakSelf.navigationController pushViewController:webVC animated:YES];

};

[headerView addSubview: scrollView];

return headerView;

}else{

#pragma mark -- 二组 标题

JXHeaderReusableView *headerView = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"JXHeader" forIndexPath:indexPath];

return headerView;

}

}

//header高度

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{

if (section == 0) {

return CGSizeMake(SCREEN_WIDTH, 202);

}else{

return CGSizeMake(SCREEN_WIDTH, 30);

}

}

//item大小

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

if (indexPath.section == 0) {

return CGSizeMake((SCREEN_WIDTH - 15)/2, SCREEN_WIDTH/2 - 110);

}else{

return CGSizeMake((SCREEN_WIDTH - 15)/2, SCREEN_WIDTH/2+100);

}

}

//调节item边距

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

return UIEdgeInsetsMake(5, 5, 0, 5);

}

#pragma mark -- item点击跳转

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

}

UICollectionView的基本使用 collectionView的更多相关文章

  1. 使用UICollectionView实现首页的滚动效果

    实现类似这样的效果,可以滚动大概有两种实现方案 1. 使用scrollview来实现 2. 使用UICollectionView来实现 第一种比较简单,而且相对于性能来说不太好,于是我们使用第二种方案 ...

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

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

  3. UICollectionView的使用小记录和一些说明

    // // MallTestViewController.h // fitmiss // // Created by bill on 16/6/28. // Copyright © 2016年 lea ...

  4. iOS开发- UICollectionView详解+实例

    本章通过先总体介绍UICollectionView及其常用方法,再结合一个实例,了解如何使用UICollectionView. UICollectionView 和 UICollectionViewC ...

  5. CollectionView 简用

    创建一个CollectionView 分为几个步骤 1.先创建布局FlowLayout 设置布局格式 2.创建CollectionView 并使用布局Flowlayout  -initWithFram ...

  6. [IOS UICollectionView模版]

    创建CollectionCell模版: 1.新建类CollectionCell继承自UICollectionViewCell 2.新建Xib,命名为CollectionCell.xib a.选中Col ...

  7. 李洪强iOS开发之UICollectionView的使用

    想做如下的界面效果(纯代码) ------------------------------------------------------------------------------------ ...

  8. iOS开发——UI篇OC篇&UICollectionView详解+实例

    UICollectionView详解+实例 实现步骤: 一.新建两个类 1.继承自UIScrollView的子类,比如HMWaterflowView * 瀑布流显示控件,用来显示所有的瀑布流数据 2. ...

  9. [转] iOS开发- UICollectionView详解+实例

    本章通过先总体介绍UICollectionView及其常用方法,再结合一个实例,了解如何使用UICollectionView. UICollectionView 和 UICollectionViewC ...

随机推荐

  1. UML在软件开发中各个阶段的作用和意义

    经典的软件工程思想将软件开发分成5个阶段:需求分析,系统分析与设计,系统实现,测试及维护五个阶段. 之所以如此,是因为软件开发中饣含了物和人的因素,存在着很大的不确定性,这使得软件工程不可能像理想的, ...

  2. MPEG-PS封装格式

    据传输媒体的质量不同,MPEG-2中定义了两种复合信息流:传送流(TS:TransportStream)和节目流(PS:ProgramStream) PS文件分为3层:ps层(Program Stre ...

  3. Contentprovider的创建和使用流程概述

    首先在provider类中继承并实现provider的几个必要方法 -- boolean onCreate(),用来执行一些初始化的工作. -- cursor query(Uri, String[], ...

  4. spirng boot web配置开发

    spring-booter-starter-web是spring-boot web发开的核心,自动配置信息存储在spring-boot-autoconfigure.jar 下面的web目录里面,包含了 ...

  5. 第5章 使用MUI与H5+构建移动端app

    H5+是JS封装的工具集合,通过H5+我们就可以使用JS的方式去调用到我们手机端上的一些原生的组件. http://dev.dcloud.net.cn/mui/ http://dev.dcloud.n ...

  6. [patl2-014]列车调度

    解题关键:由Dilworth定理(最小反链划分 == 最长链)可知最少的下降序列个数就等于整个序列最长上升子序列的长度,此题即转化为求最长上升子序列的长度. #include<cstdio> ...

  7. JavaScript 书籍推荐(转)

    作者:宋学彦链接:https://www.zhihu.com/question/19713563/answer/23068003来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  8. 框架之 hibernate之各种查询

    1. Hibernate的查询方式 2. Hibernate的查询策略 案例:使用Hibernate完成查询所有联系人功能 需求分析 1. 完成所有的联系人的查询 技术分析之Hibernate框架的查 ...

  9. OpenGL超级宝典完整源码(第五版)

    链接:https://pan.baidu.com/s/1dGQkk4T 密码:wu44 Visual Studio 2017配置OpenGL https://blog.csdn.net/qiangbi ...

  10. 项目一:第十二天 1、常见权限控制方式 2、基于shiro提供url拦截方式验证权限 3、在realm中授权 5、总结验证权限方式(四种) 6、用户注销7、基于treegrid实现菜单展示

    1 课程计划 1. 常见权限控制方式 2. 基于shiro提供url拦截方式验证权限 3. 在realm中授权 4. 基于shiro提供注解方式验证权限 5. 总结验证权限方式(四种) 6. 用户注销 ...