iOS实现UICollectionViewDataSource与Controller的分离
之前每次用到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的分离的更多相关文章
- iOS实现UITableViewDataSource与Controller的分离
写在前面 在之前的项目中好多处用到了tableView,然而之前不懂得将代理方法实现分离,所以每在一处用到tableView就要在controller中写一遍UITableViewDataSource ...
- 【IOS笔记】View Controller Basics
View Controller Basics 视图控制器基础 Apps running on iOS–based devices have a limited amount of screen s ...
- iOS UIKit:TabBar Controller
1 结构剖析 IOS中的标签导航其实是一个UITabBarController对象,其也是一个Container View Controller.UITabBarController对象创建和管理了一 ...
- iOS的多版本配置(版本分离,多环境配置)
前几天公司说一个客户要搞一个app,我说搞呗,跟我啥关系...他说,就是从咱的app上搞,什么都一样,就是一些logo啥的不一样.我一开始感觉,那就改改logo呗,后来一想,凑,百度推送,友盟统计,B ...
- iOS push与present Controller的区别
push与present都可以推出新的界面.present与dismiss对应,push和pop对应.present只能逐级返回,push所有视图由视图栈控制,可以返回上一级,也可以返回到根vc,其他 ...
- iOS开发中view controller设置问题
- 用Model-View-ViewModel构建iOS App(转)
转载自 Model-View-ViewModel for iOS [译] 如果你已经开发一段时间的iOS应用,你一定听说过Model-View-Controller, 即MVC.MVC是构建iOS a ...
- 用Model-View-ViewModel构建iOS App
如果你已经开发一段时间的iOS应用,你一定听说过Model-View-Controller,即MVC.MVC是构建iOS App的标准模式.然而,最近我已经越来越厌倦MVC的一些缺点.在本文,我将重温 ...
- Model-View-ViewModel for iOS [译]
如果你已经开发一段时间的iOS应用,你一定听说过Model-View-Controller, 即MVC.MVC是构建iOS app的标准模式.然而,最近我已经越来越厌倦MVC的一些缺点.在本文,我将重 ...
随机推荐
- 【原】FMDB源码阅读(三)
[原]FMDB源码阅读(三) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 FMDB比较优秀的地方就在于对多线程的处理.所以这一篇主要是研究FMDB的多线程处理的实现.而 ...
- MVVM模式和在WPF中的实现(二)数据绑定
MVVM模式解析和在WPF中的实现(二) 数据绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...
- Matlab slice方法和包络法绘制三维立体图
前言:在地球物理勘探,流体空间分布等多种场景中,定位空间点P(x,y,x)的物理属性值Q,并绘制三维空间分布图,对我们洞察空间场景有十分重要的意义. 1. 三维立体图的基本要件: 全空间网格化 网格节 ...
- [C#] 回眸 C# 的前世今生 - 见证 C# 6.0 的新语法特性
回眸 C# 的前世今生 - 见证 C# 6.0 的新语法特性 序 目前最新的版本是 C# 7.0,VS 的最新版本为 Visual Studio 2017 RC,两者都尚未进入正式阶段.C# 6.0 ...
- gitHub使用入门和github for windows的安装教程
在看这篇教程之前我想大家也在搜索怎样使用gitHub托管自己的项目,在使用gitHub之前我也遇到过各种问题,在网上我也搜索了很多,但总觉得网上搜索到的东西很多很杂,有的根本不知道是在表达什么.在这过 ...
- PHP获取上个月最后一天的一个容易忽略的问题
正常来说,PHP是有一个很方便的函数可以获取上个月时间的 strtotime (PHP 4, PHP 5, PHP 7) strtotime - 将任何英文文本的日期时间描述解析为 Unix 时间戳 ...
- [开发笔记] Graph Databases on developing
TimeWall is a graph databases github It be used to apply mathematic model and social network with gr ...
- jquery.cookie的使用
今天想到了要为自己的影像日记增加赞的功能,并且需要用到cookie. 记得原生的js操作cookie也不是很麻烦的,但似乎jquery更简单,不过相比原生js,需要额外引入2个文件,似乎又不是很好,但 ...
- H3 BPM让天下没有难用的流程之技术特性
一.集成性 H3 BPM可以与其它系统进行多个层面的集成,满足企业的针对不同系统的集成需求. 图:多种集成维度 Ø 用户集成 可与企业现有系统进行组织架构同步或调用,也可以直接与AD 进行集成. ...
- Linux常用命令
命令格式与目录处理命令 ls 命令格式与目录处理命令 ls 命令格式:命令 [-选项][参数] 例:ls -la /etc 说明: 1)个别命令使用不遵循格式 2)当有多个选项时,可以写在一起 3)简 ...