独立UICollectionViewDataSource OC版

之前写过一篇关于独立DataSource Swift版的文章,使用MVC架构完成UICollectionView(独立UICollectionViewDataSource)。独立DS的优点很多,最直观的就是解耦了,还有就是可以使VC的体积迅速下降,不再动辄数百行了。下面直接上内容:

Cell:

MyCollectionViewCell.h:

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface MyCollectionViewCell : UICollectionViewCell

@property (strong, nonatomic) UIImageView * imageView;

@property (strong, nonatomic) UILabel * label;

@end

NS_ASSUME_NONNULL_END

MyCollectionViewCell.m:

#import "MyCollectionViewCell.h"

@implementation MyCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIColor.whiteColor;
        
        _label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 80, 30)];
        _label.tag = 1000;
        [self.contentView addSubview:_label];
        
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(150, 10, 80, 80)];
        _imageView.tag = 1001;
        [self.contentView addSubview:_imageView];
        
    }
    return self;
}

@end

DataSource:

MyDataSource.h:

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface MyDataSource : NSObject<UICollectionViewDataSource>

@end

NS_ASSUME_NONNULL_END

MyDataSource.m:

#import "MyDataSource.h"
#import "MyCollectionViewCell.h"

@implementation MyDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell * cell = (MyCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"ID" forIndexPath:indexPath];
    
    NSLog(@"EXCUTE REUSABLE CELL.");
    
    UIImageView * imageView = [cell viewWithTag:1001];
    UIImage * image = [UIImage imageNamed:@"like_select"];
    imageView.image = image;

    UILabel * label = [cell viewWithTag:1000];
    label.text = @"ABC";
     
    cell.backgroundColor = UIColor.grayColor;
    
    return  cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 4;
}

@end

VC:

ListViewController.h:

#import <UIKit/UIKit.h>
#import "MyCollectionViewCell.h"
#import "MyDataSource.h"

NS_ASSUME_NONNULL_BEGIN

@interface ListViewController : UIViewController<UICollectionViewDelegateFlowLayout>

@end

NS_ASSUME_NONNULL_END

ListViewController.m:

#import "ListViewController.h"

@interface ListViewController ()
@property(strong, nonatomic) NSMutableArray * dataArray;
@property(strong, nonatomic) MyDataSource * myDataSource;

@end

@implementation ListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = UIColor.whiteColor;
    
    _myDataSource = [[MyDataSource alloc] init];
    
    [self initData];
    [self installCollectionView];
}

- (void) initData {
    _dataArray = [NSMutableArray arrayWithObjects:@"C", @"C++", @"Java", @"Objective-C",
                  @"C#", @"Swift", @"SQL", nil];
}

- (void) installCollectionView {
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    layout.itemSize = CGSizeMake(self.view.frame.size.width, 130);
    
    UICollectionView * cv = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
    cv.backgroundColor = UIColor.whiteColor;
    [cv registerClass:[MyCollectionViewCell self] forCellWithReuseIdentifier:@"ID"];
    
    cv.dataSource = self.myDataSource;
    cv.delegate = self;
    [self.view addSubview:cv];
}

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值