ios UICollectionView实现不同大小cell等间距

来源:互联网 发布:aisino金税盘开票软件 编辑:程序博客网 时间:2024/05/16 16:14

demo 下载地址:http://download.csdn.net/detail/chchong1234/8692121

左边对齐,右边不用对齐,实现了UICollectionView  Cell不同大小cell等间距

在此处调用EqualSpaceFlowLayout

 EqualSpaceFlowLayout *flowLayout = [[EqualSpaceFlowLayout alloc] init];    flowLayout.delegate = self;        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50, self.view.bounds.size.width, self.view.bounds.size.height - 100) collectionViewLayout:flowLayout];    self.collectionView.backgroundColor = [UIColor lightGrayColor];    self.collectionView.delegate = self;    self.collectionView.dataSource = self;    [self.view addSubview:self.collectionView];        [self.collectionView registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:@"CellIdentifier"];

自定义EqualSpaceFlowLayout  实现等间距

#import <UIKit/UIKit.h>@protocol  EqualSpaceFlowLayoutDelegate<UICollectionViewDelegateFlowLayout>@end@interface EqualSpaceFlowLayout : UICollectionViewFlowLayout@property (nonatomic,weak) id<EqualSpaceFlowLayoutDelegate> delegate;@end


#import "EqualSpaceFlowLayout.h"@interface EqualSpaceFlowLayout()@property (nonatomic, strong) NSMutableArray *itemAttributes;@end@implementation EqualSpaceFlowLayout- (id)init{    if (self = [super init]) {        self.scrollDirection = UICollectionViewScrollDirectionVertical;        self.minimumInteritemSpacing = 5;        self.minimumLineSpacing = 5;        self.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);    }        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);        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


1 0
原创粉丝点击