// SonViewController.m
// testStoryboard
#import "SonViewController.h"
@interface SonViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong)UICollectionView *collView;
@property(nonatomic,strong)UICollectionViewFlowLayout *flowLayout;
@end
@implementation SonViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title=@"father";
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemClose target:self action:@selector(popVC)];
[self.view addSubview:self.collView];
[self.collView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"itemcell"];
[self.collView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headercell"];
[self.collView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footercell"];
}
-(void)popVC{
[self.navigationController popViewControllerAnimated:YES];
}
- (UICollectionView *)collView{
if(!_collView) {
_collView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:self.flowLayout];
_collView.backgroundColor = [UIColor whiteColor];
_collView.scrollEnabled=YES;
_collView.delegate=self;
_collView.dataSource=self;
}
return _collView;
}
- (UICollectionViewFlowLayout *)flowLayout{
if (!_flowLayout) {
_flowLayout = [[UICollectionViewFlowLayout alloc] init];
_flowLayout.minimumInteritemSpacing = 20;//列间距
_flowLayout.minimumLineSpacing = 20;//列间距
_flowLayout.itemSize = CGSizeMake(50, 50);//item的大小
_flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);//每个分区的 上左下右 的内边距
_flowLayout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 50);//区头和区尾的大小
_flowLayout.footerReferenceSize = CGSizeMake(self.view.frame.size.width, 30);
_flowLayout.sectionHeadersPinToVisibleBounds = YES;//头视图和尾视图 是否始终固定在屏幕上边和下边
_flowLayout.sectionFootersPinToVisibleBounds = YES;
_flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
}
return _flowLayout;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return12;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"itemcell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor systemTealColor];
returncell;
}
- (void)collectionView:(UICollectionView*)collectionViewdidSelectItemAtIndexPath:(NSIndexPath*)indexPath{
NSLog(@"select %ld %ld",(long)indexPath.section,(long)indexPath.row);
}
#pragma mark- 头部 尾部
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headercell" forIndexPath:indexPath];
header.backgroundColor = [UIColor systemYellowColor];
returnheader;
}
UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footercell" forIndexPath:indexPath];
footer.backgroundColor = [UIColor systemGreenColor];
returnfooter;
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
UICollectionView 学习
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 原文地址:https://www.jianshu.com/p/db55bd5f5aeb[https://www.j...
- 在UICollection里面的Horizontal、Vertical 模式里面 - (CGFloat)colle...
- 数据源协议 讲的非常全的文章blog.csdn.net/sinat_34194127/article/detail...
- 转载地址Demo UICollectionView详解 section 、item 全家福:UICollectio...
- UICollectionView基础学习 iOS6引入的集合(CollectionView)是一种网格状视图, 用...