// 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)是一种网格状视图, 用...