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的一些缺点.在本文,我将重 ...
随机推荐
- JavaScript之父Brendan Eich,Clojure 创建者Rich Hickey,Python创建者Van Rossum等编程大牛对程序员的职业建议
软件开发是现时很火的职业.据美国劳动局发布的一项统计数据显示,从2014年至2024年,美国就业市场对开发人员的需求量将增长17%,而这个增长率比起所有职业的平均需求量高出了7%.很多人年轻人会选择编 ...
- win10 环境 gitbash 显示中文乱码问题处理
gitbash 是 windows 环境下非常好用的命令行终端,可以模拟一下linux下的命令如ls / mkdir 等等,如果使用过程中遇到中文显示不完整或乱码的情况,多半是因为编码问题导致的,修改 ...
- 多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类)
前言:刚学习了一段机器学习,最近需要重构一个java项目,又赶过来看java.大多是线程代码,没办法,那时候总觉得多线程是个很难的部分很少用到,所以一直没下决定去啃,那些年留下的坑,总是得自己跳进去填 ...
- async & await 的前世今生(Updated)
async 和 await 出现在C# 5.0之后,给并行编程带来了不少的方便,特别是当在MVC中的Action也变成async之后,有点开始什么都是async的味道了.但是这也给我们编程埋下了一些隐 ...
- nodejs进阶(5)—接收请求参数
1. get请求参数接收 我们简单举一个需要接收参数的例子 如果有个查找功能,查找关键词需要从url里接收,http://localhost:8000/search?keyword=地球.通过前面的进 ...
- 富文本编辑器Simditor的简易使用
最近打算自己做一个博客系统,并不打算使用帝国cms或者wordpress之类的做后台管理!自己处于学习阶段也就想把从前台到后台一起谢了.好了,废话不多说了,先来看看富文本编辑器SimDitor,这里是 ...
- [.NET] C# 知识回顾 - 委托 delegate (续)
C# 知识回顾 - 委托 delegate (续) [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/6046171.html 序 上篇<C# 知识回 ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- 【转】为什么我们都理解错了HTTP中GET与POST的区别
GET和POST是HTTP请求的两种基本方法,要说它们的区别,接触过WEB开发的人都能说出一二. 最直观的区别就是GET把参数包含在URL中,POST通过request body传递参数. 你可能自己 ...
- 设计模式C#合集--抽象工厂模式
抽象工厂,名字就告诉你是抽象的了.上代码. public interface BMW { public void Drive(); } public class BMW730 : BMW { publ ...