美文网首页
collectionView的使用总结

collectionView的使用总结

作者: xieyinghao | 来源:发表于2018-07-18 11:02 被阅读47次
    - (void)initSubViews
    {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        
        //flowLayout.itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width-15)/2, 200);
        // 设置行的最小间距
    //    flowLayout.minimumInteritemSpacing = 10;
    //    // 设置列最小行间距
    //    flowLayout.minimumLineSpacing = 10;
        // 设置cell之间的上,左,下,右
    //    flowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
        // 滚动方向
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        //    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
        // 如果未设置背景颜色是黑色设置背景颜色
        self.collectionView.backgroundColor = [UIColor whiteColor];
        // 设置代理
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"CELL"];
    
        
        [self.view addSubview:self.collectionView];
        
        UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200)];
        headView.backgroundColor = [UIColor yellowColor];
       // [self.collectionView addSubview:headView];
    
    }
    
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
        //    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];
        UICollectionView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];
        
        cell.backgroundColor = [UIColor colorWithRed:arc4random()%256/256.0 green:arc4random()%256/256.0 blue:arc4random()%256/256.0 alpha:1];
        
        
        return cell;
    }
    -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
    {
        
        return 10;
    }
    #pragma mark - 设置collectionView的大小
    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
    {
    
            //展开
        return CGSizeMake(([UIScreen mainScreen].bounds.size.width-40)/3, 150);
    }
    ////设置单元格间的横向间距
    //- (CGFloat) collectionView:(UICollectionView *)collectionView
    //                    layout:(UICollectionViewLayout *)collectionViewLayout
    //minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
    //{
    //    return 5.0f;
    //}
    ////设置纵向的行间距
    //- (CGFloat) collectionView:(UICollectionView *)collectionView
    //                    layout:(UICollectionViewLayout *)collectionViewLayout
    //minimumLineSpacingForSectionAtIndex:(NSInteger)section
    //{
    //    return 25;
    //}
    //2: 设置section间的大小
    
    - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
    
    {
    
          return UIEdgeInsetsMake(0, 10, 0, 10);
    }
    

    相关文章

      网友评论

          本文标题:collectionView的使用总结

          本文链接:https://www.haomeiwen.com/subject/nlcopftx.html