zoukankan      html  css  js  c++  java
  • collectionview使用

    创建UICollectionViewFlowLayout 对象来设置相关的布局,包括itemSize,headerReferenceSize,sectionInset。设置对应的布局大小,相关的和顶部之间的间距等。

    UICollectionView创建对应的view并且定义对应的大小,设置代理方法和数据源对象。

    -(void)loadCollectionView
    {
        UICollectionViewFlowLayout * collectionViewFlow = [[UICollectionViewFlowLayout alloc]init];
       UICollectionView *collectView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, ScreenHeight, CGRectGetWidth(_homeScrollView.frame), 2000)collectionViewLayout:collectionViewFlow];
        
        collectView.dataSource = self;
        collectView.delegate = self;
        collectView.backgroundColor = [UIColor blueColor];
        [collectView registerClass:[CSHomeCollectionViewCell class] forCellWithReuseIdentifier:@"collectionCell"];
        collectionViewFlow.itemSize = CGSizeMake(93, 120);
        collectionViewFlow.sectionInset = UIEdgeInsetsMake(40, 0, 0, 0);
        collectionViewFlow.scrollDirection  = UICollectionViewScrollDirectionVertical;
        [_homeScrollView addSubview:collectView];
    }
    //datasource,delegate
    -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
    {
        return 20;
    }
    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
          CSHomeCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
        cell.topImage.image = [UIImage imageNamed:@"cludy_bg"];
        return cell;
    }

    从而来实现对应的方法,其中自定义的cell类

    -(instancetype)initWithFrame:(CGRect)frame
    {
        self =[super initWithFrame:frame];
        if (self) {
            self.backgroundColor = [UIColor grayColor];
            self.topImage = [[UIImageView  alloc]initWithFrame:CGRectMake(5, 5, MYSIZE.width-10, MYSIZE.height*0.6)];
            [self addSubview:_topImage];
            
            self.designer = [[UILabel alloc]initWithFrame:CGRectMake(5, CGRectGetHeight(self.topImage.frame), CGRectGetWidth(self.frame), 20)];
        
            self.designer.text = @"hello";
            [self addSubview:_designer];
            
    //        self.goodBtn = [UIButton alloc]initWithFrame:CGRectMake(0, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>)
            
        }
        return self;
    }

    当定义的时候,自动调用initwithframe函数来初始化

    纸上得来终觉浅,绝知此事要躬行
  • 相关阅读:
    QT visual stuido 集成插件不能打开ui文件的原因
    KLSudoku 1.2 数独游戏软件发布
    QT for linux 的错误 undefined reference to 'FcFreeTypeQueryFace' 的解决方法
    tp3.2源码解析——入口文件
    关于php调用.net的web service 踩过的坑
    CentOS7 LNMP+phpmyadmin环境搭建(二、LNMP环境搭建)
    CentOS7 LNMP+phpmyadmin环境搭建(一、虚拟机及centos7安装)
    php无限分类
    php的文件引用
    CentOS7 LNMP+phpmyadmin环境搭建(三、安装phpmyadmin)
  • 原文地址:https://www.cnblogs.com/asheng/p/5172309.html
Copyright © 2011-2022 走看看