关于iOS UICollectionView的使用UICollectionViewFlowLayout

来源:互联网 发布:软件可以申请专利吗 编辑:程序博客网 时间:2024/06/07 08:15

1.UICollectionView使用通常伴随着布局文件的使用

当系统默认布局不满足条件的情况下,去请创建自定义类继承UICollectionViewFlowLayout


实现范例:

- (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect{
    
    NSMutableArray *attributes = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
    
    for(int i = 1; i < [attributes count]; ++i){
        //当前attributes
        UICollectionViewLayoutAttributes *currentLayoutAttributes = attributes[i];
        //上一个attributes
        UICollectionViewLayoutAttributes *prevLayoutAttributes = attributes[i - 1];
        //我们想设置的最大间距,可根据需要改
        NSInteger maximumSpacing = 0;
        //前一个cell的最右边
        NSInteger origin = CGRectGetMaxX(prevLayoutAttributes.frame);
        //如果当前一个cell的最右边加上我们想要的间距加上当前cell的宽度依然在contentSize中,我们改变当前cell的原点位置
        //不加这个判断的后果是,UICollectionView只显示一行,原因是下面所有cell的x值都被加到第一行最后一个元素的后面了
        if(origin + maximumSpacing + currentLayoutAttributes.frame.size.width < self.collectionViewContentSize.width){
            CGRect frame = currentLayoutAttributes.frame;
            frame.origin.x = origin + maximumSpacing;
            currentLayoutAttributes.frame = frame;
            
            //NSLog(@"currentLayoutAttributes.frame =%@",NSStringFromCGRect(currentLayoutAttributes.frame));
        }
    }
    
    return attributes;
}


2.对于网格布局中两个单元格间距为0时,实现范例:

#pragma mark -UICollectionViewDataSource & Delegate Methods
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake([UIScreen mainScreen].bounds.size.width/2.0, ([UIScreen mainScreen].bounds.size.width-15)/2.0+51);
}
//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0,0,0,0);
    //return UIEdgeInsetsZero;
}

//设置UICollectionView
    ProductCollectionViewFlowLayout *flowLayout=[[ProductCollectionViewFlowLayout alloc] init];
    flowLayout.minimumInteritemSpacing=0.f;//左右间隔
    flowLayout.minimumLineSpacing=0.f;
    flowLayout.sectionInset = UIEdgeInsetsZero;
    flowLayout.scrollDirection=UICollectionViewScrollDirectionVertical;


3.网格布局中使用多个单元格类型,需要分别注册

    [self.BussinessCollectionView registerClass:[AccountCommonCell class] forCellWithReuseIdentifier:@"CommonCell"];
    [self.BussinessCollectionView registerClass:[VoiseCollectionCell class] forCellWithReuseIdentifier:@"VoiseCell"];


4.关于网格布局中页眉页脚的实现问题

    //AccountHeaderView
    [self.BussinessCollectionView registerClass:[AccountHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
    
    [self.BussinessCollectionView registerClass:[AccountCommonHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CommonHeaderView"];
    [self.BussinessCollectionView registerClass:[AccountCollectionFootView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView"];


先要创建类继承UICollectionResuable类,然后分别绑定页眉页脚

在在实现代理方法中页眉页脚的具体放置

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

{

      UICollectionReusableView *reusableview = nil;

     if (kind == UICollectionElementKindSectionHeader){

         RecipeCollectionHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];

         NSString *title = [[NSString alloc] initWithFormat:@"Recipe Group #%i",indexPath.section +1];

         headerView.title.text = title;

         UIImage *headerImage = [UIImage imageNamed:@"header_banner.png"];

         headerView.backgroundImage.image = headerImage;

         reusableView = headerView;

  }

        if (kind == UICollectionElementKindSectionFooter){

    UICollectionReusableView *footerview = [collectionView dequeueResuableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];

       reusableview = footerview;

}

   return reusableview;

   

}



0 0
原创粉丝点击