UICollectionView可以用于首页复杂页面布局,当然更习惯UIScrollview自行控制

#pragma mark - dateSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return _params.allKeys.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSString *key = [NSString stringWithFormat:@"%ld",(long)section];
    NSInteger count = [_params[key] integerValue];
    return count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class]) forIndexPath:indexPath];
    NSArray *colorArr = @[[UIColor redColor],[UIColor orangeColor],[UIColor yellowColor],[UIColor greenColor],[UIColor blueColor],];
    UIColor *color = colorArr[indexPath.section];
    cell.backgroundColor = color;
    return cell;
}

// 设置cell大小 itemSize:可以给每一个cell指定不同的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *sizeArr = @[@"{375,180}",@"{375,60}",@"{375,120}",@"{375,100}",@"{187,100}"];
    NSString *str = sizeArr[indexPath.section];
    return CGSizeFromString(str);
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
   
    UIEdgeInsets top  = {1,0,0,0};
    return top;
    
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    // int interInfo = [self.showItems[section][@"type"] intValue];
    //        if (interInfo == kCellTypeFour ) {
    //            return 0;
    //        }
    return 1;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    // int interInfo = [self.showItems[section][@"type"] intValue];
    //    if (interInfo == kCellTypeFour) {
    //        return 0;
    //    }
    return 0;
}



//第二步:实现代理方法,返回区头区尾的尺寸

// 设置区头尺寸高度
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    CGSize size = CGSizeMake(375, 0);
    if (section == 3 || section == 4) {
        size = CGSizeMake(375, 44);
    }
    return size;
}
// 设置区尾尺寸高度
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    CGSize size = CGSizeMake(375, 0);
    return size;
}

//第三步:设置区头view和区尾view

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionReusableView *reusableView = nil;
    // 区头
    if (kind == UICollectionElementKindSectionHeader) {
        if (indexPath.section == 3 || indexPath.section == 4) {
            UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head" forIndexPath:indexPath];
            reusableView = headerView;
        }
    }
    // 区尾
    if (kind == UICollectionElementKindSectionFooter) {
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"foot" forIndexPath:indexPath];
        reusableView = footerView;
    }
    return reusableView;
}

#pragma mark -- get
- (UICollectionView *)collectionView{
    
    if (!_collectionView) {
        CGRect collectionViewFrame= self.view.bounds;
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        // 设置UICollectionView为垂直滚动
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        // 设置第一个cell和最后一个cell,与父控件之间的间距
        //        flowLayout.sectionInset = UIEdgeInsetsMake(0, 18, 0, 18);
        _collectionView = [[UICollectionView alloc] initWithFrame:collectionViewFrame collectionViewLayout:flowLayout];
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class])];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.showsVerticalScrollIndicator = NO;//垂直方向显示滑动条
        _collectionView.alwaysBounceVertical = YES;//垂直方向滑动
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        //第一步:需要先注册区头区尾
        
        // 注册区头
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
        // 注册区尾
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"foot"];
    }
    return _collectionView;
}

你可能感兴趣的:(UICollectionView可以用于首页复杂页面布局,当然更习惯UIScrollview自行控制)