之前每次用到UICollectionView的时候都会都需要在Controller里面去实现DataSource & Delegate方法

单独Delegate方法还好不是很多, 但是再加上DataSource就很臃肿了, 为了避免代码臃肿也减少ViewController的代码量

我们可以将DataSource方法分离出去, 大致方法如下:

-> 创建需要的Model & 自定义Cell文件

-> 创建DataSource类, 导入 Cell头文件并实现UICollectionViewDatasource

-> 在Controller中导入Model & DataSource类

-> 创建DataSource类实例, 将数据传入DataSource中

-> 创建UICollectionView, 将CollectionView的datasource指给上面创建的Datasource实例即可

下面举例示范:

为了简单 我就只下一个自定义的Cell model就不写了

ShowPhotoCollectionViewCell.h

 #import <UIKit/UIKit.h>

 @interface ShowPhotoCollectionViewCell : UICollectionViewCell

 @property (nonatomic, strong) UILabel     *lable;
@property (nonatomic, strong) UIImageView *imageView; /**
配置Cell方法 @param imageLink 图片地址
@param title 标题
*/
- (void)configCellWithImageLink:(NSString *)imageLink withTitle:(NSString *)title;
@end

ShowPhotoCollectionViewCell.m

 #import "ShowPhotoCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "UIImage+Image.h" @implementation ShowPhotoCollectionViewCell - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame];
if (self) { self.lable = ({ UILabel *lable = [[UILabel alloc] initWithFrame:\
CGRectMake((SCREEN_WIDTH - ) / , , , )]; lable.textAlignment = NSTextAlignmentCenter;
lable.font = [UIFont systemFontOfSize:];
lable.backgroundColor = [UIColor blackColor];
lable.textColor = [UIColor whiteColor]; lable;
}); self.imageView = ({ UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(, , SCREEN_WIDTH - , SCREEN_HEIGHT - - )]; imgView;
}); [self addSubview:self.lable];
[self addSubview:self.imageView];
} return self;
} - (void)configCellWithImageLink:(NSString *)imageLink withTitle:(NSString *)title { self.lable.text = title;
[self.imageView sd_setImageWithURL:[NSURL URLWithString:imageLink]
placeholderImage:[UIImage imageWithColor:[UIColor whiteColor]]];
} @end

ShowPhotoDataSource.h

 #import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> @interface ShowPhotoDataSource : NSObject <UICollectionViewDataSource> @property (nonatomic, strong) NSArray *imgLinkArray;
@property (nonatomic, strong) NSString *identifier; /**
导入外部数据 @param linkArray Image地址数组
@param identifier cell标识
@return 返回实例
*/
- (id)initWithImageLinkArray:(NSArray *)linkArray withIdentifier:(NSString *)identifier; /**
根据索引返回图片地址 @param indexPath 索引
@return 返回图片地址
*/
- (id)imgLinkAtIndexPath:(NSIndexPath *)indexPath; @end

ShowPhotoDataSource.m

 #import "ShowPhotoDataSource.h"
#import "ShowPhotoCollectionViewCell.h" @implementation ShowPhotoDataSource - (id)initWithImageLinkArray:(NSArray *)linkArray withIdentifier:(NSString *)identifier{ self = [super init];
if (self) { self.imgLinkArray = linkArray;
self.identifier = identifier;
} return self;
} - (id)imgLinkAtIndexPath:(NSIndexPath *)indexPath { return self.imgLinkArray[indexPath.row];
} #pragma mark - CollectionView dataSource Methods
- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section { return self.imgLinkArray.count;
} - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath { ShowPhotoCollectionViewCell *cell = \
[collectionView dequeueReusableCellWithReuseIdentifier:self.identifier
forIndexPath:indexPath];
[cell configCellWithImageLink:[self imgLinkAtIndexPath:indexPath]
withTitle:\
[NSString stringWithFormat:@"%d/%d", indexPath.row + , self.imgLinkArray.count]]; return cell;
} @end

下面是在Controller中的使用方法

 //创建CollectionView
- (void)createCollectionView { self.dataSource = ({ PhotoDataSource *dataSource = [[PhotoDataSource alloc] \
initWithImageLinkArray:self.imglinkArray
withIdentifier:PHOTOCELLIDENTIFIER]; dataSource;
}); self.myCollectionView = ({ UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
[layout setScrollDirection:UICollectionViewScrollDirectionHorizontal]; UICollectionView *collection = [[UICollectionView alloc] initWithFrame:\
CGRectMake(, , SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:layout]; [collection registerClass:[PhotoCollectionViewCell class]
forCellWithReuseIdentifier:PHOTOCELLIDENTIFIER]; collection.showsHorizontalScrollIndicator = NO;
collection.dataSource = self.dataSource;
collection.delegate = self; collection;
}); [self.view addSubview:self.myCollectionView];
}

iOS实现UICollectionViewDataSource与Controller的分离的更多相关文章

  1. iOS实现UITableViewDataSource与Controller的分离

    写在前面 在之前的项目中好多处用到了tableView,然而之前不懂得将代理方法实现分离,所以每在一处用到tableView就要在controller中写一遍UITableViewDataSource ...

  2. 【IOS笔记】View Controller Basics

    View Controller Basics   视图控制器基础 Apps running on iOS–based devices have a limited amount of screen s ...

  3. iOS UIKit:TabBar Controller

    1 结构剖析 IOS中的标签导航其实是一个UITabBarController对象,其也是一个Container View Controller.UITabBarController对象创建和管理了一 ...

  4. iOS的多版本配置(版本分离,多环境配置)

    前几天公司说一个客户要搞一个app,我说搞呗,跟我啥关系...他说,就是从咱的app上搞,什么都一样,就是一些logo啥的不一样.我一开始感觉,那就改改logo呗,后来一想,凑,百度推送,友盟统计,B ...

  5. iOS push与present Controller的区别

    push与present都可以推出新的界面.present与dismiss对应,push和pop对应.present只能逐级返回,push所有视图由视图栈控制,可以返回上一级,也可以返回到根vc,其他 ...

  6. iOS开发中view controller设置问题

  7. 用Model-View-ViewModel构建iOS App(转)

    转载自 Model-View-ViewModel for iOS [译] 如果你已经开发一段时间的iOS应用,你一定听说过Model-View-Controller, 即MVC.MVC是构建iOS a ...

  8. 用Model-View-ViewModel构建iOS App

    如果你已经开发一段时间的iOS应用,你一定听说过Model-View-Controller,即MVC.MVC是构建iOS App的标准模式.然而,最近我已经越来越厌倦MVC的一些缺点.在本文,我将重温 ...

  9. Model-View-ViewModel for iOS [译]

    如果你已经开发一段时间的iOS应用,你一定听说过Model-View-Controller, 即MVC.MVC是构建iOS app的标准模式.然而,最近我已经越来越厌倦MVC的一些缺点.在本文,我将重 ...

随机推荐

  1. 【探索】机器指令翻译成 JavaScript

    前言 前些时候研究脚本混淆时,打算先学一些「程序流程」相关的概念.为了不因太枯燥而放弃,决定想一个有趣的案例,可以边探索边学. 于是想了一个话题:尝试将机器指令 1:1 翻译 成 JavaScript ...

  2. [PHP内核探索]PHP中的哈希表

    在PHP内核中,其中一个很重要的数据结构就是HashTable.我们常用的数组,在内核中就是用HashTable来实现.那么,PHP的HashTable是怎么实现的呢?最近在看HashTable的数据 ...

  3. DDD CQRS架构和传统架构的优缺点比较

    明天就是大年三十了,今天在家有空,想集中整理一下CQRS架构的特点以及相比传统架构的优缺点分析.先提前祝大家猴年新春快乐.万事如意.身体健康! 最近几年,在DDD的领域,我们经常会看到CQRS架构的概 ...

  4. 继电器是如何成为CPU的(1)

    继电器是如何成为CPU的(1) ——<穿越计算机的迷雾>整理和总结 究竟是如何设计的电路,具有计算和控制的智力? 这一点也不高深.本系列文章从初中学的最简单的电路图说起,看看能不能从最初的 ...

  5. mac osx 安装redis扩展

    1 php -v查看php版本 2 brew search php|grep redis 搜索对应的redis   ps:如果没有brew 就根据http://brew.sh安装 3 brew ins ...

  6. SQLServer地址搜索性能优化例子

    这是一个很久以前的例子,现在在整理资料时无意发现,就拿出来再改写分享. 1.需求 1.1 基本需求: 根据输入的地址关键字,搜索出完整的地址路径,耗时要控制在几十毫秒内. 1.2 数据库地址表结构和数 ...

  7. Mysql存储引擎及选择方法

    0x00 Mysql数据库常用存储引擎 Mysql数据库是一款开源的数据库,支持多种存储引擎的选择,比如目前最常用的存储引擎有:MyISAM,InnoDB,Memory等. MyISAM存储引擎 My ...

  8. Java FtpClient 实现文件上传服务

    一.Ubuntu 安装 Vsftpd 服务 1.安装 sudo apt-get install vsftpd 2.添加用户(uftp) sudo useradd -d /home/uftp -s /b ...

  9. Struts2实现ajax的两种方式

    基于Struts2框架下实现Ajax有两种方式,第一种是原声的方式,另外一种是struts2自带的一个插件. js部分调用方式是一样的: JS代码: function testAjax() { var ...

  10. Nginx反向代理,负载均衡,redis session共享,keepalived高可用

    相关知识自行搜索,直接上干货... 使用的资源: nginx主服务器一台,nginx备服务器一台,使用keepalived进行宕机切换. tomcat服务器两台,由nginx进行反向代理和负载均衡,此 ...