#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. qt安装必要的库 qt开源安装包下载

    yum install mesa-libGL-devel mesa-libGLU-devel #yum install freeglut-devel http://www.qt.io/download ...

  2. DAY10-MYSQL存储引擎

    一 什么是存储引擎 mysql中建立的库===>文件夹 库中建立的表===>文件 现实生活中我们用来存储数据的文件有不同的类型,每种文件类型对应各自不同的处理机制:比如处理文本用txt类型 ...

  3. python爬虫实战(3)--图片下载器

    本篇目标 1.输入关键字能够根据关键字爬取百度图片 2.能够将图片保存到本地文件夹 1.URL的格式 进入百度图片搜索apple,这时显示的是瀑布流版本,我们选择传统翻页版本进行爬取.可以看到网址为: ...

  4. struts2学习笔记(1)配置与基本操作

    主要作用:将请求与页面区分开 配 置: 下载struts 2.0,在安装路径D:\项目学习\三大框架视屏\struts-2.3.24-all\struts-2.3.24\apps 中解压struts2 ...

  5. 使用myeclipse开发Servlet

    1.在myeclipse中创建一个web工程 2.在src目录下建立一个包并建立一个Servlet(myeclipse会自把Servlet映射到web.xml文件中) 3.发布工程,实际上就是把web ...

  6. git 本地代码到github(转)

    git 本地代码到github   一·什么是gitHub? 官网解释:gitHub是一个让无论处于何地的代码工作者能工作于同一个项目,同一个版本的平台.(GitHub is a code hosti ...

  7. vray学习笔记(5)-学习资料

    首先肯定是vray的官方的资料了: 一个是教程 https://docs.chaosgroup.com/display/VRAY3MAX/Tutorials 一个是帮助文件 https://docs. ...

  8. Python 使用其他邮件服务商的 SMTP 访问(QQ、网易、163、Google等)发送邮件

    163邮箱SMTP授权 使用Python SMTP发送邮件 # -*- coding:utf-8 -*- from __future__ import print_function __author_ ...

  9. margin,CSS边距重叠

    CSS外边距叠加就是margin-collapse,边距合并指的是,当两个垂直外边距相遇时,它们将形成一个外边距,水平边 距永远不会重合. 重叠结果计算规则: 两个相邻的外边距都是正数时,折叠结果是它 ...

  10. windows10系统连接蓝牙鼠标自动断开解决方案

    环境: Windows 10 企业版 2016 长期服务版 罗技M590 问题: 鼠标长时间未使用,会自动断开 解决步骤: 参考链接: https://zhidao.baidu.com/questio ...