iOS CollectionView 封装演变 (1.)

#import "YCHomeVC.h"

#import "YCHomeViewADCollectionCell.h"
#import "YCHomeViewVideoHeadCollectionCell.h"
#import "YCHomeViewVideoCollectionCell.h"
#import "YCHomeViewNewsHeadCollectionCell.h"
#import "YCHomeViewNewsCollectionCell.h"

@interface YCHomeVC ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@end

@implementation YCHomeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.lqs_backgroundColor = CommonViewControllerBackgroundColor;
    
    UICollectionViewFlowLayout *collectionLayout = [UICollectionViewFlowLayout new];
    collectionLayout.sectionInset = UIEdgeInsetsMake(0, 15, 15, 15);
    collectionLayout.minimumInteritemSpacing = 0;
//    collectionLayout.minimumLineSpacing = 0;
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:collectionLayout];
    collectionView.backgroundColor = [UIColor clearColor];
    collectionView.dataSource = self;
    collectionView.delegate = self;
    [collectionView registerClass:[YCHomeViewADCollectionCell class] forCellWithReuseIdentifier:@"YCHomeViewADCollectionCell"];
    [collectionView registerClass:[YCHomeViewVideoHeadCollectionCell class] forCellWithReuseIdentifier:@"YCHomeViewVideoHeadCollectionCell"];
    [collectionView registerClass:[YCHomeViewVideoCollectionCell class] forCellWithReuseIdentifier:@"YCHomeViewVideoCollectionCell"];
    [collectionView registerClass:[YCHomeViewNewsHeadCollectionCell class] forCellWithReuseIdentifier:@"YCHomeViewNewsHeadCollectionCell"];
    [collectionView registerClass:[YCHomeViewNewsCollectionCell class] forCellWithReuseIdentifier:@"YCHomeViewNewsCollectionCell"];
    [self lqs_boxControllerWithFillView:collectionView];
}

- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 10;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        YCHomeViewADCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YCHomeViewADCollectionCell" forIndexPath:indexPath];
        return cell;
    }
    if (indexPath.row == 1) {
        YCHomeViewVideoHeadCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YCHomeViewVideoHeadCollectionCell" forIndexPath:indexPath];
        return cell;
    }
    if (indexPath.row == 2 || indexPath.row == 3) {
        YCHomeViewVideoCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YCHomeViewVideoCollectionCell" forIndexPath:indexPath];
        return cell;
    }
    if (indexPath.row == 4) {
        YCHomeViewNewsHeadCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YCHomeViewNewsHeadCollectionCell" forIndexPath:indexPath];
        return cell;
    }
    
    YCHomeViewNewsCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YCHomeViewNewsCollectionCell" forIndexPath:indexPath];
    
    return cell;
}

- (CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        CGFloat width = collectionView.frame.size.width - 15 - 15;
        CGFloat height = [LQS heightWithWidth:width equalRatioWithOtherWidth:345 otherHeight:200];
        return CGSizeMake(width, height);
    }
    if (indexPath.row == 1) {
        CGFloat width = collectionView.frame.size.width - 15 - 15;
        CGFloat height = 30;
        return CGSizeMake(width, height);
    }
    if (indexPath.row == 2 || indexPath.row == 3) {
        CGFloat width = (collectionView.frame.size.width - 15 - 15 - 7) / 2;
        CGFloat height = [LQS heightWithWidth:width equalRatioWithOtherWidth:169 otherHeight:105];
        return CGSizeMake(width, height);
    }
    if (indexPath.row == 4) {
        CGFloat width = collectionView.frame.size.width - 15 - 15;
        CGFloat height = 30;
        return CGSizeMake(width, height);
    }
    
    CGFloat width = collectionView.frame.size.width - 15 - 15;
    CGFloat height = 90;
    return CGSizeMake(width, height);
}

@end

F3121D44-3B0C-45DA-AE33-C2C5497C56F5

这是最简单不过的写法。