UI第十四天

注意:

由于collectionV背景默认为黑,一般我们给他一个背景颜色

collectionV.backgroundColor = [UIColor whiteColor];


一些概念:

UICollectionView

1.遵循协议

<UICollectionViewDelegate,UICollectionViewDataSource>


2. //创建流布局

    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];

    

    CGFloat itemWH = (self.view.frame.size.width - 40)/3;

    //设置cell的大小

    flow.itemSize = CGSizeMake(itemWH, itemWH);

    //在纵向(默认滑动方向)设置的是左右cell之间的最小距离

    //在横向 设置的是上下cell之间的最小距离

    flow.minimumInteritemSpacing = 0;

    //在纵向(默认滑动方向)设置的是上下cell之间的最小距离

    //在横向 设置的是左右cell之间的最小距离

    flow.minimumLineSpacing = 10;

    //设置分组距离collectionView四周的边距

    flow.sectionInset = UIEdgeInsetsMake(20, 10, 10, 10);

    //设置头部视图的宽高

    flow.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 40);

    

    UICollectionView *collectionV = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flow];

    collectionV.tag = 1000;

    collectionV.delegate = self;

    collectionV.dataSource = self;

    //背景默认为黑

    collectionV.backgroundColor = [UIColor whiteColor];

    [self.view addSubview:collectionV];

    //注册cell

    [collectionV registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

    //注册头部复用

    //UICollectionElementKindSectionHeader 标记头部

    //UICollectionElementkindSectionFooter 标记尾部

    [collectionV registerClass:[HeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];



3.

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{

    return self.dataArray.count;

}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

    //从复用池中取cell,如果没有取到,那么系统会自动给你创建cell

    CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

    NSDictionary *dic = self.dataArray[indexPath.row];

    cell.imageV.image = [UIImage imageNamed:dic[@"userImage"]];

    cell.layer.borderWidth = 1.0;

    cell.layer.borderColor = [[UIColor blackColor]CGColor];

    cell.titleLabel.text = dic[@"userName"];

    cell.titleLabel.textAlignment = NSTextAlignmentCenter;

    return cell;

}


4.

#pragma mark ---头部尾部视图复用协议函数

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

{

    if ([kind isEqualToString:UICollectionElementKindSectionHeader])

    {

        HeaderCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];

        header.backgroundColor = [UIColor redColor];

        return header;

    }

    else

    {

        return nil;

    }

}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

{

    NSLog(@"section:%ld-----row:%ld",indexPath.section,indexPath.row);

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值