UICollectionView的flowLayout使用

2017-03-06  本文已影响1009人  起个名字真难啊2015

一般我们是如此来使用,

    //创建好flowLayout,然后设置相关的属性;
   UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 20;             //不同行间的间距
    layout.minimumInteritemSpacing = 30;      //同一列中的两个item的间距
    layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 137);  //sectionheader的size
    CGFloat itemWidth = (SCREEN_WIDTH - 70) * 0.5;
    layout.itemSize = CGSizeMake(itemWidth, itemWidth * 410 / 308.0);
    layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);     //用来给section中的内容设置边距
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) collectionViewLayout:layout];
    [self.view addSubview:self.collectionView];

但有时我们依据不同的index,会有不同的itemSize或者spacing,这时我们可以如下操作;在self.collectionViewdelegate中遵守协议UICollectionViewDelegateFlowLayout,这里只需遵守就可以了,不需要相关的代码(xxx.delegate = xxx;是不需要的)

//1,这里遵守协议
@interface LTIsCustomingViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

//2,这里实现协议中的方法
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        //50是sectionInset的距离
        return CGSizeMake(SCREEN_WIDTH - 50, 228);
    }else{
        return CGSizeMake(SCREEN_WIDTH - 50, 175);
    }
}

UICollectionViewDelegateFlowLayout这个协议还提供了其他的相关方法如下:

//定制itemSize
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;

//定制section的inset
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;

//定制lineSpacing
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;

//定制itemSpacing
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;

//定制headView的Size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;

//定制footerView的Size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;

再就是有时项目中有需要设置到headViewfooterView,不同于tableViewUICollectionView提供了UICollectionReusableView类和代理方法- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath来实现

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

  //这里通过kind来区分时section的头部还是尾部,UICollectionElementKindSectionHeader头部,UICollectionElementKindSectionFooter尾部
    if (kind == UICollectionElementKindSectionHeader) {
        //自定义继承自UICollectionReusableView的视图
        LTUserArchivesReusableView *reuserView = (LTUserArchivesReusableView*)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:identifierReuseView forIndexPath:indexPath];
        if (indexPath.section == 0) {
            reuserView.viewType = LTRuseViewTypeCommon;
            reuserView.largeImageView.image = [UIImage imageNamed:@"metou"];
            reuserView.detailLabel.text = @"2016121221";
            reuserView.titleLabel.text = @"PH清盈保湿乳";
            
        }
        return reuserView;
    }else{
        return [[UICollectionReusableView alloc] init];
    }
}
上一篇 下一篇

猜你喜欢

热点阅读