iOS开发之高级视图—— UICollectionView
2016-05-26 20:23
483 查看
UICollectionView 和 UICollectionViewController 类是iOS6 新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableView 和UITableViewController 类。
优点:
1⃣️可以高度定制内容的展现
2⃣️管理数据最佳的做法
3⃣️即使是处理大量数据,也非常的高效
UICollectionView的整体是由3个元素构成分别如下:
Cells(单元格)
Supplementary Views(补充的view,相当于TableView的页眉和页脚)
Decoration Views(装饰View,用于装饰整个UICollectionView的)
通常使用UICollectionView 需要实现如下3个协议
UICollectionViewDataSource 主要用于向Collection View提供数据
UICollectionViewDelegate
UICollectionViewDelegateFlowLayout
协议常用方法介绍
UICollectionViewDataSource
//定义显示的UICollectionViewCell的数量
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
//定义显示的Section的实例
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;
//每个UICollectionView显示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
UICollectionViewDelegateFlowLayout
//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
//定义每个UICollectionView 的 间距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;
UICollectionViewDelegate
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;
//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath;
本例子实现了一个简单的相册。
ViewController.h
//
// ViewController.h
// UICollectionViewApp
//
// Created by Apple on 16/5/26.
// Copyright © 2016年 Apple. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@end
ViewController.m
//
// ViewController.m
// UICollectionViewApp
//
// Created by Apple on 16/5/26.
// Copyright © 2016年 Apple. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
NSMutableArray* imgNames;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
imgNames = [[NSMutableArray alloc] init];
for (int i=1;i<=8; i++) {
[imgNames addObject:[NSString stringWithFormat:@"%d.jpg",i]];
}
// UICollectionViewFlowLayout布局采用"流"的方式管理UICollectionView中的所有单元格,这些单元格要么横向排列,要么纵向排列,其效果就像一个网格
UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc] init];
// UICollectionView可实现多列布局
UICollectionView* collectionView = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:flowLayout];
// 设置UICollectionView的背景颜色
[collectionView setBackgroundColor:[UIColor whiteColor]];
// 注册UICollectionViewCell
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellID"];
// 设置dataSource
collectionView.dataSource= self;
// 设置delegate
collectionView.delegate = self;
// 将UICollectionView添加到视图控制器
[self.view addSubview:collectionView];
}
//返回数据总共有多少,就是需要创建多少个Cell单元
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return [imgNames count];
}
//产生一个一个的Cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
// 获取一个UICollectionViewCell
UICollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
// 创建一个UIImageView
UIImageView* imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imgNames [indexPath.row]]];
// 设置cell的背景
cell.backgroundView = imgView;
return cell;
}
#pragma mark - UICollectionViewDelegateFlowLayout
//返回每个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(self.view.frame.size.width/2-20, self.view.frame.size.height/3-10);
}
//设置每一个Cell的垂直和水平间距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
//top
//left
//bottom
//right
return UIEdgeInsetsMake(10, 5, 10, 5);
}
@end
效果图如下:
优点:
1⃣️可以高度定制内容的展现
2⃣️管理数据最佳的做法
3⃣️即使是处理大量数据,也非常的高效
UICollectionView的整体是由3个元素构成分别如下:
Cells(单元格)
Supplementary Views(补充的view,相当于TableView的页眉和页脚)
Decoration Views(装饰View,用于装饰整个UICollectionView的)
通常使用UICollectionView 需要实现如下3个协议
UICollectionViewDataSource 主要用于向Collection View提供数据
UICollectionViewDelegate
UICollectionViewDelegateFlowLayout
协议常用方法介绍
UICollectionViewDataSource
//定义显示的UICollectionViewCell的数量
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
//定义显示的Section的实例
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;
//每个UICollectionView显示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
UICollectionViewDelegateFlowLayout
//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
//定义每个UICollectionView 的 间距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;
UICollectionViewDelegate
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;
//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath;
本例子实现了一个简单的相册。
ViewController.h
//
// ViewController.h
// UICollectionViewApp
//
// Created by Apple on 16/5/26.
// Copyright © 2016年 Apple. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@end
ViewController.m
//
// ViewController.m
// UICollectionViewApp
//
// Created by Apple on 16/5/26.
// Copyright © 2016年 Apple. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
NSMutableArray* imgNames;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
imgNames = [[NSMutableArray alloc] init];
for (int i=1;i<=8; i++) {
[imgNames addObject:[NSString stringWithFormat:@"%d.jpg",i]];
}
// UICollectionViewFlowLayout布局采用"流"的方式管理UICollectionView中的所有单元格,这些单元格要么横向排列,要么纵向排列,其效果就像一个网格
UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc] init];
// UICollectionView可实现多列布局
UICollectionView* collectionView = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:flowLayout];
// 设置UICollectionView的背景颜色
[collectionView setBackgroundColor:[UIColor whiteColor]];
// 注册UICollectionViewCell
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellID"];
// 设置dataSource
collectionView.dataSource= self;
// 设置delegate
collectionView.delegate = self;
// 将UICollectionView添加到视图控制器
[self.view addSubview:collectionView];
}
//返回数据总共有多少,就是需要创建多少个Cell单元
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return [imgNames count];
}
//产生一个一个的Cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
// 获取一个UICollectionViewCell
UICollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
// 创建一个UIImageView
UIImageView* imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imgNames [indexPath.row]]];
// 设置cell的背景
cell.backgroundView = imgView;
return cell;
}
#pragma mark - UICollectionViewDelegateFlowLayout
//返回每个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(self.view.frame.size.width/2-20, self.view.frame.size.height/3-10);
}
//设置每一个Cell的垂直和水平间距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
//top
//left
//bottom
//right
return UIEdgeInsetsMake(10, 5, 10, 5);
}
@end
效果图如下:
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用开发中AFNetworking库的常用HTTP操作方法小结
- iOS应用中UISearchDisplayController搜索效果的用法
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- iOS中UIAlertView警告框组件的使用教程
- 浅析iOS应用开发中线程间的通信与线程安全问题
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android、iOS和Windows Phone中的推送技术详解