//
// MallTestViewController.h
// fitmiss
//
// Created by bill on 16/6/28.
// Copyright © 2016年 lear. All rights reserved.
// #import "RootViewController.h" @interface MallTestViewController : RootViewController @end
//
// MallTestViewController.m
// fitmiss
//
// Created by bill on 16/6/28.
// Copyright © 2016年 lear. All rights reserved.
// #import "MallTestViewController.h" // 注意const的位置
static NSString *const cellId = @"cellId";
static NSString *const headerId = @"headerId";
static NSString *const footerId = @"footerId"; @interface MallTestViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout> @end @implementation MallTestViewController - (void)viewDidLoad {
[super viewDidLoad]; UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; UICollectionView *colView = [[UICollectionView alloc] initWithFrame:CGRectMake(, , [Function getScreenWidth], [Function getScreenHeight:self]) collectionViewLayout:layout];
colView.backgroundColor = [UIColor grayColor];
colView.dataSource = self;
colView.delegate = self;
[self.view addSubview:colView]; // 注册cell、sectionHeader、sectionFooter
[colView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellId];
[colView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerId];
[colView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerId]; } - (NSArray *)loadData
{
NSArray *arr = [NSArray arrayWithObjects:@"cell", @"cell2", @"cell3", @"cell4", @"cell5", @"cell6", @"cell7", @"cell8", @"cell9",nil];
return arr;
} #pragma mark - UICollectionViewDataSource
// 定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return ;
} // 定义每个 UICollectionView 中展示的 UICollectionViewCell 的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [[self loadData] count];
} // 每个 UICollectionView 展示的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
return cell;
} // 设置 UICollectionView 的段头段尾内容,和UITableView类似
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{ if([kind isEqualToString:UICollectionElementKindSectionHeader])
{
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerId forIndexPath:indexPath];
if(headerView == nil)
{
headerView = [[UICollectionReusableView alloc] init];
}
headerView.backgroundColor = [UIColor whiteColor]; return headerView;
}
else if([kind isEqualToString:UICollectionElementKindSectionFooter])
{
UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:footerId forIndexPath:indexPath];
if(footerView == nil)
{
footerView = [[UICollectionReusableView alloc] init];
}
footerView.backgroundColor = [UIColor lightGrayColor]; return footerView;
} return nil;
} // 是否可移动
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
} // 处理移动
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath
{ } #pragma mark - UICollectionViewDelegateFlowLayout // 定义 UICollectionView 中 cell 的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == ) {
return CGSizeMake(, );
}
return CGSizeMake(, );
} // 定义 UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(, , , );
} // 定义 UICollectionView 中上下两个 cell 的最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return .f;
} // 定义 UICollectionView 中左右两个 cell 的最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return .f;
} // 定义 UICollectionView 头部的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
if (section==) {
return CGSizeMake(, );
}
return CGSizeMake(, );
} // 定义 UICollectionView 尾部的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
return CGSizeMake(, );
} #pragma mark - UICollectionViewDelegate // UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{ NSLog(@"您点击了item:%@", [[self loadData] objectAtIndex:indexPath.row]);
UICollectionViewCell *cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
//cell.backgroundColor = [UIColor whiteColor]; } // 返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
// if (indexPath.row % 2)
// {
// return YES;
// }
// return NO;
} // 开启突出某个内容
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
} // 按下时处理内容 如:变色
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.backgroundColor = [UIColor blueColor];
} // 松开时处理内容 如:变色
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.backgroundColor = [UIColor greenColor];
} // 长按某item,弹出copy和paste的菜单
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
} // 使copy和paste有效
- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender
{
if ([NSStringFromSelector(action) isEqualToString:@"copy:"] || [NSStringFromSelector(action) isEqualToString:@"paste:"])
{
return YES;
} return NO;
} //处理copy和paste
- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender
{ // if([NSStringFromSelector(action) isEqualToString:@"copy:"])
// {
// // NSLog(@"-------------执行拷贝-------------");
// [collectionView performBatchUpdates:^{
// [_section0Array removeObjectAtIndex:indexPath.row];
// [collectionView deleteItemsAtIndexPaths:@[indexPath]];
// } completion:nil];
// }
// else if([NSStringFromSelector(action) isEqualToString:@"paste:"])
// {
// NSLog(@"-------------执行粘贴-------------");
// }
} @end

UICollectionView的使用小记录和一些说明的更多相关文章

  1. Java 打印金字塔 or 打印带数字的金字塔 (Java 学习中的小记录)

    Java 打印金字塔 or 打印带数字的金字塔 (Java 学习中的小记录) 作者:王可利(Star·星星) 效果图: 代码如下: class Star8 { public static void m ...

  2. Java 需要记得、了解的关键词 (Java 学习中的小记录)

    Java 需要记得.了解的关键词 (Java 学习中的小记录)     作者:王可利(Star·星星) 总结:本次随笔,仅且拿来平时翻阅记忆用

  3. 关于HTML中,绝对定位,相对定位的理解...(学习HTML过程中的小记录)

    关于HTML中,绝对定位,相对定位的理解...(学习HTML过程中的小记录)   作者:王可利(Star·星星) HTML中 相对定位:position:relative; 绝对定位:position ...

  4. 利用JQ实现的,高仿 彩虹岛官网导航栏(学习HTML过程中的小记录)

    利用JQ实现的,高仿 彩虹岛官网导航栏(学习HTML过程中的小记录)   作者:王可利(Star·星星) 总结: 今天学习的jQ类库的使用,代码重复的比较多需要完善.严格区分大小写,在 $(" ...

  5. html/css 盒子布局 Margin 、Padding 、border 以及 清除浮动的知识 (学习HTML过程中的小记录)

    html/css  盒子布局 Margin .Padding .border 以及 清除浮动的知识 (学习HTML过程中的小记录) 作者:王可利(Star·星星) width     是"宽 ...

  6. NDK开发小记录 C++读取java层对象内容

    这是自己NDK开发得小记录,关于C++层读取java层传来的对象内容. 很简单的一个例子, 1.首先在java层定义了一个类NumList: public class NumList { public ...

  7. centos 6.X minimal 系列最小化安装完成后,安装mono和jexus过程小记录

    在使用虚拟机安装minimal版centos运行mono+jexus的时候,遇到了一些坑,记录一下,比如虚拟机访问不了网络,没wget命令没开放80端口,等等小问题,其他网上教程已经有mono+jex ...

  8. Backbone小记录

    前言 这两天看了下Backbone.js的知识,大概了解了这个框架的一些知识. 写篇博客总结一下. Backbone.js是一个web端javascript的轻量级MVC框架.为什么说是轻量级呢?因为 ...

  9. HorizontalScrollView做页卡的一个小记录

    用HorizontalScrollView做页卡,实现一个如下图的效果:

随机推荐

  1. TeamWork-天气美食

    一.  团队情况      Hello,欢迎来到我们"Code Man"队的第一次团队作业页面,"代码侠"很明显我们是一个编程队伍,由大三在读的6位同班同学组成 ...

  2. 写简单游戏,学编程语言-python篇

    好吧, 首先得承认这个题目写的夸大了,人才菜鸟一枚,游戏相关编程也是知道点概念.但是本人对游戏开发比较感兴趣,相信大多数喜欢玩玩游戏,因为它给人确实带来很多乐趣,而编程语言的学习最少对于我来说比较乏味 ...

  3. PHP面向对象实例(图形计算器)

    效果:

  4. NOIP2016 模拟赛

    7.10 T1:求出一个矩阵中平均数大于0的子矩阵的最大面积. T2:给出一个N行的,第I行有n+1-i的倒三角形,从中选取m个数,只有当前数的左上角和右上角都被选是才能选当前数,求选的数字的最大和 ...

  5. ajax请求!

    ajax请求: var data ='{"useName":"'+name+'",}' $.ajax({ type:"post", url: ...

  6. linux动态时钟探索

    在早期的linux内核版本的时间概念都是由周期时钟提供的.虽然比较有效,但是,对于关注能耗电量的系统上,就不能满足长时间休眠的需求,因为周期系统要求必须在一定的频率下,周期性的处于活动状态.因此,li ...

  7. extjs4_msg

    经过一早上的努力,终于成功显示了ext.msg.alert("somestring")!一个漂亮的对话框,呵呵 真的是感想挺多,收获也很多的.主要是: 一.用Wamp在win7下搭 ...

  8. linux下JDK1.7安装

    http://mnt.conf.blog.163.com/blog/static/115668258201210793915876/ 一.软件下载1.下载JDK(下面分别是32位系统和64位系统下的版 ...

  9. 105 董婷婷 第一次Sprint总结

    总结: 经过一个多星期,第一次Sprint在今天结束了,在这次Sprint中我们的工作主要做的是对项目的构建及各种界面的设计和完善,一些比较具体的功能还没有实现.在这次Sprint中因为有上学期合作的 ...

  10. C#使用quartz.net定时问题

    因工作需要需要完成定时查询数据..因此在了解之后完成了一个demo 所需要的dll在该地址下载 http://pan.baidu.com/s/1sjNQLXV 首先引入quartz这个dll... 在 ...