1、collectionView的相关概念
datasource:数据源
delegate:代理对象
layout:布局对象,iOS自定义的布局子类flowLayout
2、CollectionView 的组成
cells(单元格)用于展示内容的主体,不同的cell可以指定不同尺寸和不同的内容
Decoration Views(装饰视图)每个section的背景
.m文件中
#import "ViewController.h"
@interface ViewController () <UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.itemSize = CGSizeMake(150, 200);
flowLayout.minimumInteritemSpacing = 50;
flowLayout.minimumLineSpacing = 10;
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
UICollectionView *cv = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
cv.dataSource = self;
cv.delegate = self;
//注册
[cv registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
[self.view addSubview:cv];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 61;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor blueColor];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
view.backgroundColor = [UIColor orangeColor];
cell.selectedBackgroundView = view;
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end