美文网首页iOS Developer
iOS实现UICollectionViewDataSource与

iOS实现UICollectionViewDataSource与

作者: Jackey_Zhou | 来源:发表于2016-12-27 11:22 被阅读205次

之前每次用到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 - 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;

/**
 导入外部数据
 
 @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 + 1 , 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(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];
}

相关文章

网友评论

    本文标题:iOS实现UICollectionViewDataSource与

    本文链接:https://www.haomeiwen.com/subject/xngsvttx.html