#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. spring @Resource与@Autowired注解详解

    具有依赖关系的Bean对象,利用下面任意一种注解都可以实现关系注入: 1)@Resource (默认首先按名称匹配注入,然后类型匹配注入) 2)@Autowired/@Qualifier (默认按类型 ...

  2. ubuntu系统里vi编辑器时,按方向箭头输入是乱码的ABCD字母?(图文详解)

    不多说,直接上干货! 问题详情 ubuntu系统里vi编辑器时,按方向箭头输入是乱码的ABCD字母?  解决办法 是由于预装的vim软件没更新,运行   sudo apt-get install vi ...

  3. AudioManager 音量『转』

    获取系统音量 通过程序获取android系统手机的铃声和音量.同样,设置铃声和音量的方法也很简单! 设置音量的方法也很简单,AudioManager提供了方法: publicvoidsetStream ...

  4. ubuntu16部署gitlab

    一.gitlab的安装 1. 安装依赖包 $ sudo apt-get update #如无ssh还需安装openssh-server $ sudo apt-get install postfix c ...

  5. 返回键的复写onBackPressed()介绍

    本篇文章是对Android中返回键的复写onBackPressed()进行了详细的分析介绍,需要的朋友参考下 在android开发中,当不满足触发条件就按返回键的时候,就要对此进行检测.尤其是当前Ac ...

  6. Angular问题01 创建组件时报错、HammerJS找不到

    1 利用ng创建组件时出现错误 1.1 ng g c test/testHome 1.2 问题描述 当angular应用中有多个module.ts文件时,创建组件时会出现冲突,因为有多个module. ...

  7. C++笔记--异常

    引言 异常,让一个函数可以在发现自己无法处理的错误时抛出一个异常,希望它的调用者可以直接或者间接处理这个问题.而传统错误处理技术,检查到一个局部无法处理的问题时: 1.终止程序(例如atol,atoi ...

  8. [学习笔记]_exit和exit深入理解

    #include<stdio.h> #include<stdlib.h> #include<string.h> #include <unistd.h> ...

  9. Jmeter-BeanShell的使用介绍

    最近学习使用了jmeter来对接口进行测试.使用jmter进行接口测试,有时候需要编写一些BeanShell脚本语言,或者利用BeanShell调用自己的工具类,来完成jmeter基本功能中无法实现的 ...

  10. Web Server Jexus配置及使用

    Web Server  Jexus配置及使用 一.jexus概念: Jexus 即 Jexus Web Server,简称JWS,是Linux平台上的一款ASP.NET WEB服务器,是 Linux. ...