UICollectionView的使用

第一部分,三个协议方法,先介绍两个


前两个的用法和tableView的很像,第三个是头视图尾视图的协议。 (头视图尾视图,一样要写代理,写注册,缺少了就不行。) 注册以后,就不需要再去管理复用的问题了。这点就很简单。这个如果用好的话,会非常的简单。很多事情迎刃而解,否则使用tableView的话,需要三个tableView一起滑动,彼此之间需要观察,一旦变化随之变化,用scroller 的ContentOffset 来控制滑动,非常不科学,用这个的话三个就一起滑动了。

第二部分,构建

先得创建一个layout,UICollectionViewFlowLayout 这个类型的


//创建布局对象
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//设置单元格的尺寸
flowLayout.itemSize = CGSizeMake(80, 80);
//设置头视图高度
flowLayout.headerReferenceSize = CGSizeMake(0, 30);
//flowlaout的属性,横向滑动
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//接下来就在创建collectionView的时候初始化,就很方便了(能直接带上layout)
_myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 200, 320, 280) collectionViewLayout:flowLayout];
_myCollectionView.tag = 200;
_myCollectionView.backgroundColor = [UIColor greenColor]; _myCollectionView.delegate = self;
_myCollectionView.dataSource = self;
//添加到主页面上去 
[self.view addSubview:_myCollectionView];
//collectionCell的注册
[_myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"myheheIdentifier"];
//collection头视图的注册   奇葩的地方来了,头视图也得注册
[_myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Identifierhead”];

#pragma mark -UICollectionViewDataSource
//指定组的个数 ,一个大组!!不是一排,是N多排组成的一个大组(与下面区分)
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}

//指定单元格的个数 ,这个是一个组里面有多少单元格,e.g : 一个单元格就是一张图片
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 10;
}

//构建单元格
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView.tag == 200) {
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myheheIdentifier" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor purpleColor];
        return cell;
    }
}

//组的头视图创建
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Identifierhead" forIndexPath:indexPath];
    headView.backgroundColor = [UIColor blueColor];
    return headView;
}

//通过协议方法设置单元格尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat rd = arc4random()%90;
    return CGSizeMake(90, rd);
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值