之前每次用到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
1 #import <UIKit/UIKit.h> 2 3 @interface ShowPhotoCollectionViewCell : UICollectionViewCell 4 5 @property (nonatomic, strong) UILabel *lable; 6 @property (nonatomic, strong) UIImageView *imageView; 7 8 /** 9 配置Cell方法 10 11 @param imageLink 图片地址 12 @param title 标题 13 */ 14 - (void)configCellWithImageLink:(NSString *)imageLink withTitle:(NSString *)title; 15 @end
ShowPhotoCollectionViewCell.m
1 #import "ShowPhotoCollectionViewCell.h" 2 #import "UIImageView+WebCache.h" 3 #import "UIImage+Image.h" 4 5 @implementation ShowPhotoCollectionViewCell 6 7 - (id)initWithFrame:(CGRect)frame { 8 9 self = [super initWithFrame:frame]; 10 if (self) { 11 12 self.lable = ({ 13 14 UILabel *lable = [[UILabel alloc] initWithFrame: 15 CGRectMake((SCREEN_WIDTH - 40) / 2, 40, 40, 25)]; 16 17 lable.textAlignment = NSTextAlignmentCenter; 18 lable.font = [UIFont systemFontOfSize:12]; 19 lable.backgroundColor = [UIColor blackColor]; 20 lable.textColor = [UIColor whiteColor]; 21 22 lable; 23 }); 24 25 self.imageView = ({ 26 27 UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(40, 80, SCREEN_WIDTH - 80, SCREEN_HEIGHT - 80 - 80)]; 28 29 imgView; 30 }); 31 32 [self addSubview:self.lable]; 33 [self addSubview:self.imageView]; 34 } 35 36 return self; 37 } 38 39 - (void)configCellWithImageLink:(NSString *)imageLink withTitle:(NSString *)title { 40 41 self.lable.text = title; 42 [self.imageView sd_setImageWithURL:[NSURL URLWithString:imageLink] 43 placeholderImage:[UIImage imageWithColor:[UIColor whiteColor]]]; 44 } 45 46 @end
ShowPhotoDataSource.h
1 #import <Foundation/Foundation.h> 2 #import <UIKit/UIKit.h> 3 4 @interface ShowPhotoDataSource : NSObject <UICollectionViewDataSource> 5 6 @property (nonatomic, strong) NSArray *imgLinkArray; 7 @property (nonatomic, strong) NSString *identifier; 8 9 /** 10 导入外部数据 11 12 @param linkArray Image地址数组 13 @param identifier cell标识 14 @return 返回实例 15 */ 16 - (id)initWithImageLinkArray:(NSArray *)linkArray withIdentifier:(NSString *)identifier; 17 18 /** 19 根据索引返回图片地址 20 21 @param indexPath 索引 22 @return 返回图片地址 23 */ 24 - (id)imgLinkAtIndexPath:(NSIndexPath *)indexPath; 25 26 @end
ShowPhotoDataSource.m
1 #import "ShowPhotoDataSource.h" 2 #import "ShowPhotoCollectionViewCell.h" 3 4 @implementation ShowPhotoDataSource 5 6 - (id)initWithImageLinkArray:(NSArray *)linkArray withIdentifier:(NSString *)identifier{ 7 8 self = [super init]; 9 if (self) { 10 11 self.imgLinkArray = linkArray; 12 self.identifier = identifier; 13 } 14 15 return self; 16 } 17 18 - (id)imgLinkAtIndexPath:(NSIndexPath *)indexPath { 19 20 return self.imgLinkArray[indexPath.row]; 21 } 22 23 #pragma mark - CollectionView dataSource Methods 24 - (NSInteger)collectionView:(UICollectionView *)collectionView 25 numberOfItemsInSection:(NSInteger)section { 26 27 return self.imgLinkArray.count; 28 } 29 30 31 - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView 32 cellForItemAtIndexPath:(NSIndexPath *)indexPath { 33 34 ShowPhotoCollectionViewCell *cell = 35 [collectionView dequeueReusableCellWithReuseIdentifier:self.identifier 36 forIndexPath:indexPath]; 37 [cell configCellWithImageLink:[self imgLinkAtIndexPath:indexPath] 38 withTitle: 39 [NSString stringWithFormat:@"%d/%d", indexPath.row + 1 , self.imgLinkArray.count]]; 40 41 return cell; 42 } 43 44 @end
下面是在Controller中的使用方法
1 //创建CollectionView 2 - (void)createCollectionView { 3 4 self.dataSource = ({ 5 6 PhotoDataSource *dataSource = [[PhotoDataSource alloc] 7 initWithImageLinkArray:self.imglinkArray 8 withIdentifier:PHOTOCELLIDENTIFIER]; 9 10 dataSource; 11 }); 12 13 self.myCollectionView = ({ 14 15 UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; 16 [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal]; 17 18 UICollectionView *collection = [[UICollectionView alloc] initWithFrame: 19 CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:layout]; 20 21 [collection registerClass:[PhotoCollectionViewCell class] 22 forCellWithReuseIdentifier:PHOTOCELLIDENTIFIER]; 23 24 collection.showsHorizontalScrollIndicator = NO; 25 collection.dataSource = self.dataSource; 26 collection.delegate = self; 27 28 collection; 29 }); 30 31 [self.view addSubview:self.myCollectionView]; 32 }