之前每次用到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. 梅须逊雪三分白,雪却输梅一段香——CSS动画与JavaScript动画

    CSS动画并不是绝对比JavaScript动画性能更优越,开源动画库Velocity.js等就展现了强劲的性能. 一.两者的主要区别 先开门见山的说说两者之间的区别. 1)CSS动画: 基于CSS的动 ...

  2. 理解Maven中的SNAPSHOT版本和正式版本

    Maven中建立的依赖管理方式基本已成为Java语言依赖管理的事实标准,Maven的替代者Gradle也基本沿用了Maven的依赖管理机制.在Maven依赖管理中,唯一标识一个依赖项是由该依赖项的三个 ...

  3. TypeScript为Zepto编写LazyLoad插件

    平时项目中使用的全部是jQuery框架,但是对于做webapp来说jQuery太过于庞大,当然你可以选择jQuery 2.*针对移动端的版本. 这里我采用移动端使用率比较多的zepto框架,他跟jqu ...

  4. 免费开源的.NET多类型文件解压缩组件SharpZipLib(.NET组件介绍之七)

    前面介绍了六种.NET组件,其中有一种组件是写文件的压缩和解压,现在介绍另一种文件的解压缩组件SharpZipLib.在这个组件介绍系列中,只为简单的介绍组件的背景和简单的应用,读者在阅读时可以结合官 ...

  5. Postman接口调试神器-Chrome浏览器插件

    首先大家可以去这个地址下载 Postman_v4.1.3 这个版本,我用的就是这个版本 http://chromecj.com/web-development/2014-09/60/download. ...

  6. GJM : C#设计模式(1)——单例模式

    感谢您的阅读.喜欢的.有用的就请大哥大嫂们高抬贵手"推荐一下"吧!你的精神支持是博主强大的写作动力以及转载收藏动力.欢迎转载! 版权声明:本文原创发表于 [请点击连接前往] ,未经 ...

  7. HTML5笔记2——HTML5音/视频标签详解

    音视频的发展史 早期:<embed>+<object>+文件 问题:不是所有浏览器都支持,而且embed不是标准. 现状:Realplay.window media.Quick ...

  8. H3 BPM产品安装手册(.Net版本)

    1         安装说明 1.1    服务器安装必备软件 在使用该工作流软件之前,有以下一些软件是必须安装: l  IIS7.0以上版本(必须): l  .Net Framework 4.5(必 ...

  9. 关于MJRefresh的下拉加载数据bug

    当没有更多数据的时候显示NoMoreData 我的理解是先结束刷新再显示没有更多 今天之前一直没发现有问题 贴之前的代码 [self.collectionView reloadData]; [self ...

  10. 敏捷转型历程 - Sprint4 回顾会

    我: Tech Leader 团队:团队成员分布在两个城市,我所在的城市包括我有4个成员,另外一个城市包括SM有7个成员.另外由于我们的BA离职了,我暂代IT 的PO 职位.PM和我在一个城市,但他不 ...