单独设置UicollectionView的一个cell的宽度

1.想单独改变一个cell的宽度,与其他的cell宽度不同,并且保持间距相等,需要封装一个BaseCollectionViewFlowLayout类


.h文件


#import <UIKit/UIKit.h>


@protocol BaseCollectionViewFlowLayoutDelegate <UICollectionViewDelegateFlowLayout>



@end

@interface BaseCollectionViewFlowLayout : UICollectionViewFlowLayout


@property (nonatomic, weak) id<BaseCollectionViewFlowLayoutDelegate> delegate;

@end





.m文件

//
//  BaseCollectionViewFlowLayout.m
//  一号车库
//
//  Created by eric on 16/7/13.
//  Copyright © 2016年 eric. All rights reserved.
//  控制cell的位置,左对齐


#import "BaseCollectionViewFlowLayout.h"


@interface BaseCollectionViewFlowLayout()
@property (nonatomic, strong) NSMutableArray *itemAttributes;
@end


@implementation BaseCollectionViewFlowLayout
- (id)init
{
    if (self = [super init]) {
        self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        self.minimumInteritemSpacing = 0;
        self.minimumLineSpacing = 0;
        self.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    
    return self;
}


#pragma mark - Methods to Override
- (void)prepareLayout
{
    [super prepareLayout];
    
    NSInteger itemCount = [[self collectionView] numberOfItemsInSection:0];
    self.itemAttributes = [NSMutableArray arrayWithCapacity:itemCount];
    
    CGFloat xOffset = self.sectionInset.left;
    CGFloat yOffset = self.sectionInset.top;
    CGFloat xNextOffset = self.sectionInset.left;
    for (NSInteger idx = 0; idx < itemCount; idx++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:idx inSection:0];
        CGSize itemSize = [self.delegate collectionView:self.collectionView layout:self sizeForItemAtIndexPath:indexPath];
        
        xNextOffset+=(self.minimumInteritemSpacing + itemSize.width);
#pragma mark - 注释的是超过屏幕宽度就换行的
//        if (xNextOffset > [self collectionView].bounds.size.width - self.sectionInset.right) {
//            xOffset = self.sectionInset.left;
//            xNextOffset = (self.sectionInset.left + self.minimumInteritemSpacing + itemSize.width);
//            yOffset += (itemSize.height + self.minimumLineSpacing);
//        }
//        else
//        {
            xOffset = xNextOffset - (self.minimumInteritemSpacing + itemSize.width);
//        }
        
        UICollectionViewLayoutAttributes *layoutAttributes =
        [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        
        layoutAttributes.frame = CGRectMake(xOffset, yOffset, itemSize.width, itemSize.height);
        [_itemAttributes addObject:layoutAttributes];
    }
}


- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return (self.itemAttributes)[indexPath.item];
}


- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    return [self.itemAttributes filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(UICollectionViewLayoutAttributes *evaluatedObject, NSDictionary *bindings) {
        return CGRectIntersectsRect(rect, [evaluatedObject frame]);
    }]];
}


- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
    return NO;
}


@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值