之前每次用到UICollectionView的時候都會都需要在Controller里面去實現(xiàn)DataSource & Delegate方法
單獨Delegate方法還好不是很多, 但是再加上DataSource就很臃腫了, 為了避免代碼臃腫也減少ViewController的代碼量
我們可以將DataSource方法分離出去, 大致方法如下:
-> 創(chuàng)建需要的Model & 自定義Cell文件
-> 創(chuàng)建DataSource類, 導(dǎo)入 Cell頭文件并實現(xiàn)UICollectionViewDatasource
-> 在Controller中導(dǎo)入Model & DataSource類
-> 創(chuàng)建DataSource類實例, 將數(shù)據(jù)傳入DataSource中
-> 創(chuàng)建UICollectionView, 將CollectionView的datasource指給上面創(chuàng)建的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 標(biāo)題
*/
- (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 - 40) / 2, 40, 40, 25)];
lable.textAlignment = NSTextAlignmentCenter;
lable.font = [UIFont systemFontOfSize:12];
lable.backgroundColor = [UIColor blackColor];
lable.textColor = [UIColor whiteColor];
lable;
});
self.imageView = ({
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(40, 80, SCREEN_WIDTH - 80, SCREEN_HEIGHT - 80 - 80)];
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;
/**
導(dǎo)入外部數(shù)據(jù)
@param linkArray Image地址數(shù)組
@param identifier cell標(biāo)識
@return 返回實例
*/
- (id)initWithImageLinkArray:(NSArray *)linkArray withIdentifier:(NSString *)identifier;
/**
根據(jù)索引返回圖片地址
@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 + 1 , self.imgLinkArray.count]];
return cell;
}
@end
下面是在Controller中的使用方法
//創(chuàng)建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(0, 0, 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];
}