您的位置:首页 > 产品设计 > UI/UE

UICollectionViewController既有headerView又有footerView。

2016-01-25 08:37 435 查看
#import "FirstHeaderReusableView.h"

#import "FirstFooterReusableView.h"

#define ITEMWIDTH ([UIScreen mainScreen].bounds.size.width - 30) / 2

#define ITEMHEIGHT 180

@interface OneViewController ()

@property (nonatomic, strong)FirstHeaderReusableView *header;

@end

@implementation OneViewController

static NSString * const reuseIdentifier = @"cell";

//懒加载创建数组

- (NSMutableArray *)dataSource{

if (!_dataSource) {

self.dataSource = [NSMutableArray arrayWithCapacity:0];

}

return _dataSource;

}

- (NSMutableArray *)rollpic{

if (!_rollpic) {

self.rollpic = [NSMutableArray arrayWithCapacity:0];

}

return _rollpic;

}

- (instancetype)init{

//创建网格化布局方式

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];

//设置布局属性

flowLayout.itemSize = CGSizeMake(ITEMWIDTH, ITEMHEIGHT);

flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);

//给header和footer腾个地儿~

flowLayout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 200);

flowLayout.footerReferenceSize = CGSizeMake(SCREEN_WIDTH, 200);

self = [self initWithCollectionViewLayout:flowLayout];

if (self) {

// insert code here...

}

return self;

}

- (void)viewDidLoad {

[super viewDidLoad];

self.collectionView.backgroundColor = [UIColor whiteColor];

//注册对应的cell

// [self.collectionView registerClass:[FirstCustomViewCell class] forCellWithReuseIdentifier:@"cell"];

[self.collectionView registerNib:[UINib nibWithNibName:@"HomePageViewCell" bundle:nil] forCellWithReuseIdentifier:reuseIdentifier];

//注册页眉或者页脚

[self.collectionView registerClass:[FirstHeaderReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];

[self.collectionView registerClass:[FirstFooterReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"footer"];

}

//返回每个分区的页眉或者页脚

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

UICollectionReusableView *reusableview = nil;

if (kind == UICollectionElementKindSectionHeader) {

//返回页眉

self.header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];

reusableview = self.header;

}

if (kind == UICollectionElementKindSectionFooter){

FirstFooterReusableView *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];

reusableview = footerview;

}

return reusableview;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: