iOS-CollectionView 基础

CollectionView基础使用方法:

我们将完成以下效果

Simulator Screen Shot 2016年5月20日 下午5.03.15.png

主要步骤如下:

在延展中把 collectionView 作为属性

@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong) UICollectionView *collectionView;

@end

//创建 layout(此处创建的是流水布局)

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

//行距

flowLayout.minimumLineSpacing = 10;

//列距

flowLayout.minimumInteritemSpacing = 10;

//设置每个 item 的大小

flowLayout.itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width -40)/3, ([UIScreen mainScreen].bounds.size.height-80)/4);

//设置 item 的上左下右的边距大小

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

//设置 UICollectionView 的滑动方向

flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;

注册 item

    //注册 item
 [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CellIdentifier];
    
    //注册头部区域
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:Header];
    
    //注册尾部区域
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:Footer];

实现UICollectionViewDataSource 协议中必须实现的方法

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 20;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
   UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.contentView.backgroundColor = [UIColor redColor];
    return cell;
}

实现UICollectionViewDelegate 协议中的一些方法

//该方法用于设置 collectionView 的 header 和 footer
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    //设置头部或尾部 view
    if (kind == UICollectionElementKindSectionHeader) {
        
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:Header forIndexPath:indexPath];
        headerView.backgroundColor = [UIColor orangeColor];
        return headerView;
    }
    else{
        
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:Footer forIndexPath:indexPath];
        footerView.backgroundColor = [UIColor yellowColor];
        return footerView;
    }
}

//点击 cell 时调用响应的方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"didselected = %lu",indexPath.row);
}
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 最近将 UICollectionView 进行了一个全面的学习及总结,参考了网上大量的文章,把官方文档进行了大概翻...
    varlarzh阅读 22,339评论 3赞 93
  • 翻译自“Collection View Programming Guide for iOS” 0 关于iOS集合视...
    lakerszhy阅读 4,165评论 1赞 22
  • 原文地址 定义collectionView的布局类型,流式布局 let layout = UICollection...
    GeniusLi阅读 2,640评论 0赞 3
  • 针对于UICollectionView的使用方法与UITableView类似 UICollectionViewLa...
    songsongchen阅读 1,712评论 1赞 2
  • 缘于你。缘于这一场早已预言的雪。 今夜注定无眠。你注定是我思念的全部。 听时钟每一次准确地响过。你的微笑在此时出现...
    竹园斋主人阅读 472评论 0赞 0

友情链接更多精彩内容