之前每次用到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. 将表里的数据批量生成INSERT语句的存储过程 增强版

    将表里的数据批量生成INSERT语句的存储过程 增强版 有时候,我们需要将某个表里的数据全部或者根据查询条件导出来,迁移到另一个相同结构的库中 目前SQL Server里面是没有相关的工具根据查询条件 ...

  2. 【社工】NodeJS 应用仓库钓鱼

    前言 城堡总是从内部攻破的.再强大的系统,也得通过人来控制.如果将入侵直接从人这个环节发起,那么再坚固的防线,也都成为摆设. 下面分享一个例子,利用应用仓库,渗透到开发人员的系统中. 应用仓库 应用仓 ...

  3. ABP文档 - 目录

    ABP框架 概览 介绍 多层结构 模块系统 启动配置 多租户 集成OWIN 共同结构 依赖注入 会话 缓存 日志 设置管理 时间 领域层 实体 值对象(新) 仓储 领域服务 工作单元 领域事件(Eve ...

  4. [译]ZOOKEEPER RECIPES-Leader Election

    选主 使用ZooKeeper选主的一个简单方法是,在创建znode时使用Sequence和Ephemeral标志.主要思想是,使用一个znode,比如"/election",每个客 ...

  5. 2.WindowsServer2012R2装完的一些友好化设置

    网站部署之~Windows Server | 本地部署 http://www.cnblogs.com/dunitian/p/4822808.html#iis 1.桌面图标(控制面板里面屏蔽了,得自己输 ...

  6. Win10 IIS本地部署MVC网站时不能运行?

    异常处理汇总-服 务 器 http://www.cnblogs.com/dunitian/p/4522983.html 部署后出现这个错误: 打开文件目录后发现是可以看见目录的,静态页面也是可以打开的 ...

  7. 使用EF CodeFirst 创建数据库

    EntityFramework 在VS2015添加新建项时,选择数据->ADO.NET 实体数据模型,有一下选项 来自数据库的EF设计器,这个就是我们最常用的EntityFramework设计模 ...

  8. Autofac - 生命周期

    实例生命周期决定在同一个服务的每个请求的实例是如何共享的. 当请求一个服务的时候,Autofac会返回一个单例 (single instance作用域), 一个新的对象 (per lifetime作用 ...

  9. 如何搭建git服务器

    一.前言 现在越来越多的公司用git进行版本控制,不过git是默认是开源的,如果私有的话是需要付费的,如果不想付费自己可以搭建一个git服务器用来版本控制. 二.服务器端操作 1.安装git sudo ...

  10. Handler

    1.1 继承AbstractController优点:能定制请求方式 package cn.happyl.controller; import javax.servlet.http.HttpServl ...