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 ...
随机推荐
- Dev控件GridControl实现CheckBox列和ComBox列
1.在sql语句中添加空白行,如select c1,c2 null c3 from xxx; 2.将sql语句查询结果与gdc绑定CmmFrm.BestFitGridViewColumnsWidth( ...
- 2016.5.30实现透明Panel及控件置顶的方法
想放置一个透明Panel在某控件上端,实现效果是可透过此Panel看见下面控件,但鼠标点击却无任何反应. 1.新建置自定义Panel类 using System; using System.Colle ...
- nginx upstream的几种配置方式
nginx 的upstream目前支持4种方式的分配 1.轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器 ,如果后端服务器down掉,能自动剔除. 2.weight指定轮询几率,weigh ...
- Acviticy.this 和 getApplicationContext()的区别
用AlertDialog 举例 AlertDialog对象是依赖于一个View的,而View是和一个Activity对应的,在Activity销毁的时候它也就销毁了,不会再存在.Activity.th ...
- 关于fragment生命周期的两张图片
图1,fragment的生命周期,图2,fragment生命周期对应activity的生命周期
- Codeforces 914C Travelling Salesman and Special Numbers (数位DP)
题意:题目中定义了一种运算,把数字x变成数字x的二进制位数.问小于n的恰好k次运算可以变成1的数的个数(题目中的n是二进制数,n最大到2^1000) 思路:容易发现,无论多么大的数,只要进行了一次运算 ...
- [转]SQL 模糊查询
在进行数据库查询时,有完整查询和模糊查询之分. 一般模糊查询语句如下: SELECT 字段 FROM 表 WHERE 某字段 Like 条件 其中关于条件,SQL提供了四种匹配模式: 1,% :表 ...
- 【转】nginx禁止访问某个文件和目录(文件夹)
nginx禁止访问所有.开头的隐藏文件设置 location ~* /.* {deny all;} nginx禁止访问目录, 例如:禁止访问path目录 location ^~ /path {deny ...
- 算法Sedgewick第四版-第1章基础-020一按优先级计算表达式的值
/****************************************************************************** * Compilation: javac ...
- rest-framework组件 之 认证与权限组件
浏览目录 认证组件 权限组件 频率组件 认证与权限组件 认证组件 局部视图认证 在app01.service.auth.py: class Authentication(BaseAuthenticat ...