iOS--集合视图CollectionView

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


结果图:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值