#import "ViewController.h"
#import "CollectionViewCell.h"
#import "CollectionReusableView.h"
@interface ViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.minimumInteritemSpacing = 0;
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
collectionView.backgroundColor = [UIColor whiteColor];
collectionView.delegate = self;
collectionView.dataSource = self;
[self.view addSubview:collectionView];
[collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([CollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:@"cell"];
[collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([CollectionReusableView class]) bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headcell"];
}
#pragma mark - 数据源
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 4;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if (section == 0) {
return 3;
} else if(section ==3) {
return 20;
}
return 4;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
if (indexPath.section == 0 && indexPath.row == 1) {
cell.frame = CGRectMake(self.view.frame.size.width / 3 , 30, self.view.frame.size.width*2 / 3 -10, 50);
}else if(indexPath.section == 0 && indexPath.row == 2){
cell.frame = CGRectMake(self.view.frame.size.width / 3, 90, self.view.frame.size.width*2 / 3-10, 50);
}
return cell;
}
#pragma mark - cell的样式
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGSize itemSize;
if (indexPath.section == 0) {
if (indexPath.row == 0) {
itemSize = CGSizeMake(self.view.bounds.size.width/3-30, 110);
}else
{
itemSize = CGSizeMake(self.view.bounds.size.width/3-30, 50);
}
return itemSize;
} else if (indexPath.section == 3) {
itemSize = CGSizeMake(self.view.bounds.size.width/3-20, 200);
return itemSize;
} else {
float viewW = self.view.frame.size.width / 3 - 10;
CGSize itemSize1 = CGSizeMake(viewW, 50);
return itemSize1;
}
}
#pragma mark - 头部样式
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
UICollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headcell" forIndexPath:indexPath];
return reusableView;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
return CGSizeMake(self.view.frame.size.width, 30);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(0, 10, 0, 10);
}
网友评论