zoukankan      html  css  js  c++  java
  • CollectionView的基础代码

    #import "ViewController.h"
    
    #define CellIdentifier @"CellIdentifier"
    
    #define Header @"Header"
    
    #define Footer @"Footer"
    
    
    @interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
    
    @property (nonatomic,strong) UICollectionView *collectionView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        //
        //创建 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;
        
        //如果有头部view,设置这个sise
        
        flowLayout.headerReferenceSize = CGSizeMake(0, 20);
        
        //如果有尾部view,设置这个sise
        
        flowLayout.footerReferenceSize = CGSizeMake(0, 50);
        
        self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
        self.collectionView.delegate =self;
        self.collectionView.dataSource =self;
        [self.view addSubview:self.collectionView];
        
        //注册 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];
        
    }
    
    #pragma mark ------------------------------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;
    }
    
    #pragma mark -----------------------------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);
    }
  • 相关阅读:
    桌面图标有蓝底
    创建与删除SQL约束或字段约束
    (转)ASP.NET(C#) 读取EXCEL ——另加解决日期问题
    ASP连接11种数据库的语法
    GridView 合并列、行类
    Office对应ContentType
    (解决办法)ASP.NET导出Excel,打开时提示“您尝试打开文件'XXX.xls'的格式与文件扩展名指定文件不一致
    (转)js判断只能输入数字或小数点
    vscode设置字体大小
    springsecurity+jwt实现登录
  • 原文地址:https://www.cnblogs.com/iOSDeng/p/9075965.html
Copyright © 2011-2022 走看看