UICollectionView

RootViewController.h

@interface RootViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

RootViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    //1.创建布局对象
    UICollectionViewFlowLayout *flowLayOut = [[UICollectionViewFlowLayout alloc] init];
    //设置滑动方向
    flowLayOut.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    //设置item的尺寸
    flowLayOut.itemSize = CGSizeMake(150, 150);
    
    //设置相同行的item之间的最小间隙
    flowLayOut.minimumInteritemSpacing = 10;
    
    //设置每一行之间的距离
    flowLayOut.minimumLineSpacing = 5;
    
    //设置头视图的尺寸
    flowLayOut.headerReferenceSize = CGSizeMake(100, 40);
    
    //2.创建collectionView
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) collectionViewLayout:flowLayOut];
    collectionView.backgroundColor = [UIColor greenColor];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self.view addSubview:collectionView];
    //3.注册item
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell110"];
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"section_head"];
}

#pragma mark - UICollectionView dataSource
//设置组的个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 2;
}
//设置每一组有多少个item
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return 30;
}

//设置组的头视图,尾视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {

    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"section_head" forIndexPath:indexPath];
    
    if (kind == UICollectionElementKindSectionHeader) {
        headerView.backgroundColor = [UIColor grayColor];
    }else if (kind == UICollectionElementKindSectionFooter) {
        headerView.backgroundColor = [UIColor redColor];
    }
    
    return headerView;
}

//创建item
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *iden = @"cell110";
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:iden forIndexPath:indexPath];
    
    NSString *name = [NSString stringWithFormat:@"%d@2x",indexPath.row+1];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:name]];
    
//    cell.contentView  添加子视图
    cell.backgroundView = imageView;
//    cell.selectedBackgroundView
    
    return cell;
}

//使用代理方法设置每一item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    int c = arc4random()%90;
    
    return CGSizeMake(c+50, c+30);
}

//单元格的点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%@",indexPath);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值