UICollectionView的基本使用 collectionView
#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的更多相关文章
- 使用UICollectionView实现首页的滚动效果
实现类似这样的效果,可以滚动大概有两种实现方案 1. 使用scrollview来实现 2. 使用UICollectionView来实现 第一种比较简单,而且相对于性能来说不太好,于是我们使用第二种方案 ...
- UICollectionLayout布局 —— UIKit之学习UICollectionView记录二《流水布局》
重点知识 一. 加载collectionView注意事项 1.创建collectionView,有两种方式 :一种是xib和一种是纯代码:设置代理和数据源,注册cell,配置流水布局的属性,如上.下. ...
- UICollectionView的使用小记录和一些说明
// // MallTestViewController.h // fitmiss // // Created by bill on 16/6/28. // Copyright © 2016年 lea ...
- iOS开发- UICollectionView详解+实例
本章通过先总体介绍UICollectionView及其常用方法,再结合一个实例,了解如何使用UICollectionView. UICollectionView 和 UICollectionViewC ...
- CollectionView 简用
创建一个CollectionView 分为几个步骤 1.先创建布局FlowLayout 设置布局格式 2.创建CollectionView 并使用布局Flowlayout -initWithFram ...
- [IOS UICollectionView模版]
创建CollectionCell模版: 1.新建类CollectionCell继承自UICollectionViewCell 2.新建Xib,命名为CollectionCell.xib a.选中Col ...
- 李洪强iOS开发之UICollectionView的使用
想做如下的界面效果(纯代码) ------------------------------------------------------------------------------------ ...
- iOS开发——UI篇OC篇&UICollectionView详解+实例
UICollectionView详解+实例 实现步骤: 一.新建两个类 1.继承自UIScrollView的子类,比如HMWaterflowView * 瀑布流显示控件,用来显示所有的瀑布流数据 2. ...
- [转] iOS开发- UICollectionView详解+实例
本章通过先总体介绍UICollectionView及其常用方法,再结合一个实例,了解如何使用UICollectionView. UICollectionView 和 UICollectionViewC ...
随机推荐
- 第三章 Java内存模型(上)
本章大致分为4部分: Java内存模型的基础:主要介绍内存模型相关的基本概念 Java内存模型中的顺序一致性:主要介绍重排序和顺序一致性内存模型 同步原语:主要介绍3个同步原语(synchroized ...
- 【转】Android下面打印进程函数调用堆栈(dump backtrace)的方法
1. 为什么要打印函数调用堆栈? 打印调用堆栈可以直接把问题发生时的函数调用关系打出来,非常有利于理解函数调用关系.比如函数A可能被B/C/D调用,如果只看代码,B/C/D谁调用A都有可能,如果打印出 ...
- DAY3-python函数
目录 一.了解函数 二. 函数定义 三.函数使用原则:先定义,后调用 四.定义函数的三种形式 五.函数的调用 六.函数的参数 七. 函数对象 八.函数嵌套 九.名称空间与作用域 十. 闭包函数 十一. ...
- 用JS实现,图片放大和缩小
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- java之控制多幅图片
package xxj.thread0904; import java.awt.Image; import javax.swing.ImageIcon; import javax.swing.JFra ...
- JAVA对Excel文件进行操作
JAVA EXCEL API:是一开放源码项目,通过它Java开发人员可以读取Excel文件的内容.创建新的Excel文件.更新已经存在的Excel文件.使用该API非Windows操作系统也可以通过 ...
- 每天一道算法题(12)——和为n的连续正数序列或者随机数
题目:输入一个正数n,输出所有和为n 连续正数序列.例如输入15,由于1+2+3+4+5=4+5+6=7+8=15,所以输出3 个连续序列1-5.4-6 和7-8. 1.思路 尊崇以下策略: (1)对 ...
- Tensorflow学习练习-卷积神经网络应用于手写数字数据集训练
# coding: utf-8 import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_data mn ...
- EZOJ #79
传送门 分析 在经过若干次操作之后一定会产生一堆环 而我们又发现从一个点到另一个点实际可以经过所有环 于是问题就转换成了$k_1s_1 + k_2s_2 + ... + len = t$ 其中$s_i ...
- dreamweaver cs5 快捷键
撤销上一步:ctrl + Z: 回复上一步:ctrl + Y: 代码缩进:左下角(应用原格式)