UIKit 框架之UICollectionView
1.自定义UICollectionViewCell
在myCollectionViewCell.h中声明两个属性
// // myCollectionViewCell.h // UICollectionView // // Created by City--Online on 15/5/25. // Copyright (c) 2015年 XQB. All rights reserved. // #import <UIKit/UIKit.h> @interface myCollectionViewCell : UICollectionViewCell @property(nonatomic,strong) UIImageView *myImageView; @property(nonatomic,strong) UILabel *nameLabel; @end
在myCollectionViewCell.m中绘制视图
//
// myCollectionViewCell.m
// UICollectionView
//
// Created by City--Online on 15/5/25.
// Copyright (c) 2015年 XQB. All rights reserved.
//
#import "myCollectionViewCell.h"
@implementation myCollectionViewCell
-(void)layoutSubviews
{
self.myImageView.frame=CGRectMake(0, 0, self.contentView.frame.size.width,80);
[self.contentView addSubview:self.myImageView];
self.nameLabel.frame = CGRectMake(0,80 , self.contentView.frame.size.width, 40);
[self.contentView addSubview:self.nameLabel];
[self.nameLabel setBackgroundColor:[UIColor cyanColor]];
}
@end
2.实例化UICollectionView,并实现它的代理方法
//
// ViewController.m
// UICollectionView
//
// Created by City--Online on 15/5/25.
// Copyright (c) 2015年 XQB. All rights reserved.
//
#import "ViewController.h"
#import "myCollectionViewCell.h"
@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong) UICollectionView *collectionView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//UICollectionViewLayout可以说是UICollectionView的大脑和中枢,它负责了将各个cell、Supplementary View和Decoration Views进行组织,为它们设定各自的属性
//Layout决定了UICollectionView是如何显示在界面上的。在展示之前,一般需要生成合适的UICollectionViewLayout子类对象,并将其赋予CollectionView的collectionViewLayout属性
UICollectionViewFlowLayout *collectionViewFlowLayout=[[UICollectionViewFlowLayout alloc]init];
collectionViewFlowLayout.minimumInteritemSpacing=0.0;
collectionViewFlowLayout.minimumLineSpacing=0.0;
collectionViewFlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
collectionViewFlowLayout.sectionInset = UIEdgeInsetsMake(0.0, 0.0, 0, 0.0);
//它定义了每一个item的大小。通过设定itemSize可以全局地改变所有cell的尺寸,如果想要对某个cell制定尺寸,可以使用-collectionView:layout:sizeForItemAtIndexPath:方法
collectionViewFlowLayout.itemSize=CGSizeMake(120, 120);
collectionViewFlowLayout.estimatedItemSize=CGSizeMake(120, 120);
//由属性scrollDirection确定scroll view的方向,将影响Flow Layout的基本方向和由header及footer确定的section之间的宽度
collectionViewFlowLayout.scrollDirection=UICollectionViewScrollDirectionVertical;
//Header和Footer尺寸 同样地分为全局和部分。需要注意根据滚动方向不同,header和footer的高和宽中只有一个会起作用。垂直滚动时section间宽度为该尺寸的高,而水平滚动时为宽度起作用
collectionViewFlowLayout.headerReferenceSize=CGSizeMake(100, 40);
collectionViewFlowLayout.footerReferenceSize=CGSizeMake(100, 40);
_collectionView=[[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:collectionViewFlowLayout];
_collectionView.backgroundColor=[UIColor whiteColor];
_collectionView.delegate=self;
_collectionView.dataSource=self;
_collectionView.allowsSelection=YES;
_collectionView.allowsMultipleSelection=YES;
// 注册相关类
[_collectionView registerClass:[myCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
[_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header"];
[_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer"];
//也可以用Nib
// [[_collectionView registerNib:<#(UINib *)#> forCellWithReuseIdentifier:<#(NSString *)#>]
// _collectionView registerNib:<#(UINib *)#> forSupplementaryViewOfKind:<#(NSString *)#> withReuseIdentifier:<#(NSString *)#>
[self.view addSubview:_collectionView];
}
//UICollectionViewDataSource 代理
//每节单元格数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
if (section==0) {
return 6;
}
else
{
return 4;
}
}
//节数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 2;
}
//单元格
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
myCollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor colorWithRed:arc4random()%256/ 255.0 green:arc4random()%256 / 255.0 blue:arc4random()% 256 / 255.0 alpha:1];
cell.myImageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1.png"]];
cell.nameLabel=[[UILabel alloc]init];
cell.nameLabel.text=[NSString stringWithFormat:@"%ld %ld",indexPath.section,indexPath.row];
return cell;
}
//节头节尾
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
if ([kind isEqualToString: UICollectionElementKindSectionFooter]) {
UICollectionReusableView *footer=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Footer" forIndexPath:indexPath];
footer.backgroundColor=[UIColor yellowColor];
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
label.text=@"label";
label.textAlignment=NSTextAlignmentCenter;
[footer addSubview:label];
return footer;
}
else
{
UICollectionReusableView *Header=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Header" forIndexPath:indexPath];
Header.backgroundColor=[UIColor blueColor];
return Header;
}
}
//UICollectionViewDelegate 代理
//选中时是否高亮
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row%2==0) {
return YES;
}
return NO;
}
//选中高亮显示后
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"didHighlightItemAtIndexPath");
}
// 允许被选中
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
//允许被取消
- (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
//选中某个单元格
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"didSelectItemAtIndexPath section=%ld row=%ld",indexPath.section,indexPath.row);
}
//取消某个单元格
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"didDeselectItemAtIndexPath section=%ld row=%ld",indexPath.section,indexPath.row);
}
//高亮不显示
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"didUnhighlightItemAtIndexPath");
}
//即将展示UICollectionViewCell
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"willDisplayCell");
}
//即将显示section的footer和header
- (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)view forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"willDisplaySupplementaryView");
}
//UICollectionViewCell显示完成
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"didEndDisplayingCell");
}
//显示section的footer和header完成
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingSupplementaryView:(UICollectionReusableView *)view forElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"didEndDisplayingSupplementaryView");
}
//是否显示 copy、cut、paste等
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
//是否显示action辅助功能
- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
SEL sel=@selector(copy:);
if (sel==action) {
return YES;
}
return NO;
}
//判断选择的是什么action
- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
SEL sel=@selector(copy:);
if (sel==action) {
NSLog(@"aaaaa");
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
在上面的Header、Footer没有自定义,其实也可以自定义。UICollectionView中的一些方法和UITableView中的类似,这里就不举例了。



UIKit 框架之UICollectionView的更多相关文章
- iOS开发概述UIkit动力学,讲述UIKit的Dynamic特性,UIkit动力学是UIkit框架中模拟真实世界的一些特性。
转发:http://my.oschina.net/u/1378445/blog/335014 iOS UIKit动力学 Dynamics UIAttachmentBehavior 实现iMessage ...
- UIKit框架使用总结--看看你掌握了多少
一.经常使用的,基本就是每次项目迭代都需要使用的 UIView.UILabel.UIImage.UIColor.UIFont.UIImageView.UITextField.UIButton. UIS ...
- Swift - 重写UIKit框架类的init初始化方法(以UITabBarController为例)
原来写了篇文章讲UITabBarController的用法,当时是从UIViewController跳转到UITabBarController页面,代码如下: 1 self.presentViewCo ...
- UIKit框架
在今后的应用程序构建中,会陆续使用各式各样的控件,因此UIKit框架的引入是必不可少的! 一.简介 UIKitk框架提供一系列的Class(类)来建立和管理iPhone OS应用程序的用户界面接口.应 ...
- iOS学习32之UIKit框架-可视化编程-XIB
1. Interface Builder 可视化编程 1> 概述 GUI : 图形用户界面(Graphical User Interface, 简称GUI, 又称图形化界面) 是指采用图形方式显 ...
- 基础框架Fundation和UIkit框架的定义和使用
Foundation 框架为所有应用程序提供基本的系统服务 您的应用程序以及 UIKit 和其他框架,都建立在 Foundation 框架的基础结构之上.Foundation 框架提供许多基本的对象类 ...
- iOS开发UIKit框架-可视化编程-XIB
1. Interface Builder 可视化编程 1> 概述 GUI : 图形用户界面(Graphical User Interface, 简称GUI, 又称图形化界面) 是指采用图形方式显 ...
- 79、iOS 的Cocoa框架、Foundation框架以及UIKit框架
Cocoa框架是iOS应用程序的基础 1. Cocoa是什么? Cocoa是 OS X和ios 操作系统的程序的运行环境. 是什么因素使一个程序成为Cocoa程序呢?不是编程语言,因为在Cocoa开发 ...
- UIKit 框架之UIView二
下面这些都是UIView一些基本的东西,具体的可以参考UIKit 框架之UIView一博客 一.自定义一个View // // MyView.m // UIView // // Created by ...
随机推荐
- [翻译]NUnit---Range and Repeat Attributes(十五)
RangeAttribute (NUnit 2.5) Range特性用于为参数话测试方法的参数的值范围指定一个值,与Random特性一样,NUnit会将每个参数的值组合为一些了测试用例,所以如果为一个 ...
- s11 day 102 python Linux环境安装 与路飞项目 微信平台接口
1.微信公众号平台沙箱环境地址 https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login 二.结算中心业务 结算中心: -购物车,删 ...
- JavaScript学习知识点归纳
JavaScript学习包括几大方面: 1.基础语法 2.JavaScript核心对象 3.DOM操作 4.BOM操作 5.正则表达式 6.AJAX 7.面向对象编程 以下依次为各版块相关内容==&g ...
- leetcode 121. 买卖股票的最佳时机 JAVA
题目: 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不能在买入股票前卖出股票 ...
- Bootstrap框架(二)
day58 巨幕 这是一个轻量.灵活的组件,它能延伸至整个浏览器视口来展示网站上的关键内容. Hello, world! This is a simple hero unit, a simple ju ...
- php主要用于哪几方面
1,服务端脚本,网站和web应用程序,web服务器,php解析器,web浏览器 2,命令行脚本 3,编写桌面应用程序
- 石头剪刀布Java实现
java实现石头剪刀布过程 首先来看石头剪刀布的所有可能情况,具体如下图 第一种思路是穷举所有可能,使用if条件语句,略显呆板和麻烦. 第二种思路,因为计算机存的是数字,所以我们可以从数字角度来找规律 ...
- 北大POJ题库使用指南
原文地址:北大POJ题库使用指南 北大ACM题分类主流算法: 1.搜索 //回溯 2.DP(动态规划)//记忆化搜索 3.贪心 4.图论 //最短路径.最小生成树.网络流 5.数论 //组合数学(排列 ...
- Java Maven项目的一些补充
写在前面 在多人开发团队中,规范是非常重要的.To explain it,就像是秦始皇统一货币.度量衡一样,主要的目的是为了降低项目技术架构差异所带来的交流成本,这样开发者就可以尽可能把注意力放在业务 ...
- JDK源码学习之 java.util.concurrent.automic包
一.概述 Java从JDK1.5开始提供了java.util.concurrent.atomic包,方便程序员在多线程环境下无锁的进行原子操作.原子变量的底层使用了处理器提供的原子指令,但是不同的CP ...