利用UIViewController,UIView,UICollectionViewCell,通过重写UICollectionViewDelegate,UICollectionViewDelegateFlowLayout, 协议的一些方法实现(以及UICollectionViewDataSource)
1,在RootViewController里面
//设置代理
self.rtView.coll.dataSource = self;
self.rtView.coll.delegate = self;
//注册cell
[self.rtView.coll registerClass:[myCell class] forCellWithReuseIdentifier:kCollCell];
//在设置header时要用到
//注册header
[self.rtView.coll registerClass:[UICollectionReusableView class]
forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
withReuseIdentifier:kHeaderView];
//注册footer
[self.rtView.coll registerClass:[UICollectionReusableView class]
forSupplementaryViewOfKind:UICollectionElementKindSectionFooter
withReuseIdentifier:kFooterView];
//返回cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
myCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollCell
forIndexPath:indexPath];
cell.backgroundColor = [UIColor colorWithRed:kColorChange green:kColorChange
blue:kColorChange alpha:1];
return cell;
}
//设置header和footer
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)
indexPath
{
if (kind == UICollectionElementKindSectionHeader) {
UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:
UICollectionElementKindSectionHeader withReuseIdentifier:kHeaderView
forIndexPath:indexPath];
header.backgroundColor = [UIColor yellowColor];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100,
header.bounds.size.height)];
label.text = @"哈哈";
[header addSubview:label];
return header;
}else{
UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:
UICollectionElementKindSectionFooter withReuseIdentifier:kFooterView
forIndexPath:indexPath];
return footer;
}
}
2,在RootView里面
//视图布局
-(void)addAllViews
{
//1,创建UICollectionViewFlowLayout
//对cell进行布局控制
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//设置
//1.1,设置大小
flowLayout.itemSize = CGSizeMake(80, 100);
//1.2,设置间距(如果给定的间距,无法满足屏幕宽度,设置无效)
flowLayout.minimumInteritemSpacing = 10;
//1.3,设置行间距
flowLayout.minimumLineSpacing = 10;
//1.4,设置滑动方向
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
//1.5,heater
flowLayout.headerReferenceSize = CGSizeMake(self.bounds.size.width, 30);
//1.6,footer
flowLayout.footerReferenceSize = CGSizeMake(self.bounds.size.width, 100);
//1.7,设置字内边框
flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
//2,创建集合视图
self.coll = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen
mainScreen].bounds.size.width, CGRectGetHeight(self.frame)) collectionViewLayout:
flowLayout];
self.coll.backgroundColor = [UIColor whiteColor];
[self addSubview:self.coll];
}
3,MyCell里面(继承于UICollectionViewCell)
//视图布局
-(void)addAllViews
{
//视图空间加载在contentView上
self.imV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,
self.contentView.bounds.size.width, self.contentView.bounds.size.height-20)];
self.imV.backgroundColor = [UIColor redColor];
[self.contentView addSubview:self.imV];
self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.imV.frame),
self.contentView.bounds.size.width, self.contentView.bounds.size.height -
CGRectGetHeight(self.imV.frame))];
self.myLabel.text = @"nice";
self.myLabel.textAlignment = NSTextAlignmentCenter;
[self.contentView addSubview:self.myLabel];
}
//一旦改变,重新显示
-(void)layoutSubviews
{
[super layoutSubviews];
self.imV.frame = CGRectMake(0, 0, self.contentView.bounds.size.width,
self.contentView.bounds.size.height-20);
self.myLabel.frame = CGRectMake(0, CGRectGetMaxY(self.imV.frame),
self.contentView.bounds.size.width, self.contentView.bounds.size.height -
CGRectGetHeight(self.imV.frame));
}