iOS开发之UICollectionViewController

1、概述

UICollectionView控件主要是用来做九宫格的,类似于android中的GridView控件。其用法与UITableView一样,首先要使控制器遵守数据源协议,再将控制器设置为UICollectionView的数据源。同样,控制器遵守了UICollectionView的代理后也可以实现代理方法等。

2、常用的数据源方法

设置UICollectionViewController一共有多少组:

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView

*)collectionView;

设置每组有多少单元格:

- (NSInteger)collectionView:(UICollectionView *)collectionView

numberOfItemsInSection:(NSInteger)section;

设置每个单元格显示的内容:

- (UICollectionViewCell *)collectionView:(UICollectionView

*)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;

3、常用的代理方法

设置每个单元格点击事件:

- (void)collectionView:(UICollectionView *)collectionView

didSelectItemAtIndexPath:(NSIndexPath *)indexPath

{

MJProduct *p = self.products[indexPath.item];

NSLog(@"点击了---%@", p.title);

}

4UICollectionViewController必须调用的方法

(1)注册cell(告诉collectionView将来创建怎样的cell)

[self.collectionView registerClass:[UICollectionViewCell class]

forCellWithReuseIdentifier:@"product"];

在调用- (UICollectionViewCell *)collectionView:(UICollectionView

*)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;设置每个单元格显示的内容(UICollectionViewCell)之前必须注册cell,一般在viewDidLoad中调用上面方法注册cell。

例如:

- (void)viewDidLoad

{

[super viewDidLoad];

// 1.注册cell(告诉collectionView将来创建怎样的cell)

UINib *nib = [UINib nibWithNibName:@"MJProductCell" bundle:nil];

[self.collectionView registerNib:nib forCellWithReuseIdentifier:

MJProductCellID];//通过xid自定义的cell

/*

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:MJProductCellID];//使用默认的UICollectionViewCell

*/

// 2.设置collectionView的背景色

self.collectionView.backgroundColor = [UIColor whiteColor];

}

 (2)从缓存池中取出cell

- (UICollectionViewCell *)collectionView:(UICollectionView

*)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

UICollectionViewCell *cell =

[collectionView dequeueReusableCellWithReuseIdentifier:@"product" forIndexPath:indexPath];

return cell;

}

在从缓存池取UICollectionViewCell重用时,不用再判断缓存池是否为空了,它与UITableView不同的是UICollectionView在缓存池没有找到cell时会自动创建新的cell。

(3)重写init方法,创建布局参数

- (id)init

{

// 1.流水布局

UICollectionViewFlowLayout *layout =

[[UICollectionViewFlowLayout alloc] init];

// 2.每个cell的尺寸

layout.itemSize = CGSizeMake(80, 80);

// 3.设置cell之间的水平间距

layout.minimumInteritemSpacing = 0;

// 4.设置cell之间的垂直间距

layout.minimumLineSpacing = 10;

// 5.设置所有cell组成的一个整体与屏幕(ViewController)四周距离

layout.sectionInset =

UIEdgeInsetsMake(layout.minimumLineSpacing, 0, 0, 0);

return [super initWithCollectionViewLayout:layout];

}

如果不创建布局参数程序会报错。一般在重写控制器的init方法中创建。

5UICollectionViewFlowLayout

UICollectionViewFlowLayout称为”流水布局”, 用来约束cell的显示。

常见属性:

Cell的尺寸:

@property (nonatomic) CGSize itemSize;

cell之间的水平间距:

@property (nonatomic) CGFloat minimumInteritemSpacing;

cell之间的垂直间距:

@property (nonatomic) CGFloat minimumLineSpacing;

四周的内边距:

@property (nonatomic) UIEdgeInsets sectionInset;

时间: 2024-08-06 05:07:59

iOS开发之UICollectionViewController的相关文章

IOS开发之copy的问题

copy的目的就是修改副本,修改原始对象和副本时不会产生干扰. 定义一个不可变属性A,再定义一个可变属性B.用B做添加删除等操作后再将B赋值给A时,有些人习惯用A = B:其实这样是不安全的. 假设有下面的一段代码: ? 1 2 3 4 5 6 7 8 9 10   int main() {    NSMutableString *strM = [NSMutableString [email protected]"123"];    NSString *str = strM;    N

iOS开发之WKWebView简单使用和常用使用场景

iOS开发之 WKWebVeiw使用 想用UIWebVeiw做的,但是突然想起来在iOS8中出了一个新的WKWebView,算是UIWebVeiw的升级版.本着对新事物的好奇,就上网查了一下,但是找了好多个都没说的多了详细,于是就问谷歌,找文档,看看使用方法,试用了一下,果然不错,记录下来,大家分享! WKWebView的特点: 性能高,稳定性好,占用的内存比较小, 支持JS交互 支持HTML5 新特性 可以添加进度条(然并卵,不好用,还是习惯第三方的). 支持内建手势, 据说高达60fps的刷

iOS开发之Auto Layout入门

随着iPhone6与iOS8的临近,适配的问题讲更加复杂,最近学习了一下Auto Layout的使用,与大家分享.  什么是Auto Layout? Auto Layout是iOS6发布后引入的一个全新的布局特性,其目的是弥补以往Autoresizing在布局方面的不足之处,以及未来面对更多尺寸适配时界面布局可以更好的适应. 为什么要用Auto Layout? Autolayout能解决不同屏幕(iPhone4,iPhone5,iPad...)之间的适配问题. 在iPhone4时代开发者只需要适

iOS开发之CocoaPods的使用

透明色:00ff00ff //设置柱状图的颜色                ColorSet cs = new ColorSet();                cs.Id = "colorset1"; #region 设置柱状图的颜色 待开发                    string strColor = oYAXIS.Color;                    switch (strColor)                    {           

iOS开发之UILabel

UILabel是iOS开发中常用的一个组件,主要用来显示内容. UILabel的主要使用如下: ? 1 2 3 4 5 6 7 8 9 10 /*尺寸*/ CGRect labelRect = CGRectMake(100, 100, 80, 40); /*初始化*/ UILabel *titleLabel = [[UILabel alloc] initWithFrame:labelRect]; /*一些属性的设置*/ titleLabel.font = [UIFont systemFontOf

iOS开发之多XIB之间相互关联

Xib link Xib 1.直接加载xib中的UIView 创建一个View1.xib, 随便设一个背景色,加一个标识UILabel, 这样好知道是这个view是哪一个view. 你可以在这个view上加作意的subview,我只是说明原理,所以这儿并没有加作何subview. 最终我的View1如下图: 由于View1会放到其它View上作为subview,所以这儿size是Freeform, Status Bar是:None. 将下面代码放到viewDidLoad中: &1这行代码就是加载

iOS开发之UISearchBar初探

iOS开发之UISearchBar初探 UISearchBar也是iOS开发常用控件之一,点进去看看里面的属性barStyle.text.placeholder等等.但是这些属性显然不足矣满足我们的开发需求.比如:修改placeholder的颜色.修改UISearchBar上面的UITextfield的背景颜色.修改UITextfield上面的照片等等. 为了实现上述的需求,最好写一个UISearchBar的子类就叫LSSearchBar吧 LSSearchBar.h如下: #import <U

iOS开发之MVVM在项目中的应用

今天写这篇博客是想达到抛砖引玉的作用,想与大家交流一下思想,相互学习,博文中有不足之处还望大家批评指正.本篇博客的内容沿袭以往博客的风格,也是以干货为主,偶尔扯扯咸蛋(哈哈~不好好工作又开始发表博客啦~). 由于本人项目经验有限,关于架构设计方面的东西理解有限,我个人对MVVM的理解主要是借鉴于之前的用过的MVC的Web框架~在学校的时候用过ThinkPHP框架,和SSH框架,都是MVC的架构模式,今天MVVM与传统的MVC可谓是极为相似,也可以说是兄弟关系,也就是一家人了. 说到架构设计和团队

iOS开发之int,NSInteger,NSUInteger,NSNumber的使用

1.首先先了解下NSNumber类型: 苹果官方文档地址:https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/Reference/Reference.html NSNumber是NSValue的一个子类,它是一个对象来存储数字值包括bool型,它提供了一系列的方法来存储char a signed or unsigned char, short