之前写过一篇关于独立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