UICollectionViewCell等间距

// 创建SelectCollectionLayout继承与UICollectionViewFlowLayout

// .h

#import <UIKit/UIKit.h>

@interface SelectCollectionLayout : UICollectionViewFlowLayout

@property (nonatomic,strong)NSMutableDictionary *resoultDic;

@end
// .m

#import "SelectCollectionLayout.h"

@implementation SelectCollectionLayout

- (instancetype)init
{
    if (self = [super init]) {
    }
    return self;
}

- (void)prepareLayout
{
    [super prepareLayout];
    self.minimumInteritemSpacing = 15;
}

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{
    // 获取所有元素,如有header 或者footer 也会包括
    NSMutableArray* attributes = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
    self.resoultDic = [[NSMutableDictionary alloc]init];
    // 设置cell 等间距,根据需求可以修改
    NSInteger maximumSpacing = 15;
    for (int i = 0; i < [attributes count]; i++) {
        UICollectionViewLayoutAttributes *currentLayoutAttributes = attributes[i];
        // 排除 header footer
        if (currentLayoutAttributes.representedElementKind != nil) {
            continue;
        }
        // 获取 每个cell 的 y 值, 将所有的具有相同 y值的cell 加入数组 组合成 Key- Value
        NSString *currentWidth = [NSString stringWithFormat:@"%f",currentLayoutAttributes.frame.origin.y];
        if (![self.resoultDic.allKeys containsObject:currentWidth]) {
            NSMutableArray *dataArray = [[NSMutableArray alloc]init];
            [self.resoultDic setObject:dataArray forKey:currentWidth];
        }
        NSMutableArray *array = self.resoultDic[currentWidth];
        [array addObject:currentLayoutAttributes];
    }
    // 取出每一行cells,将第一个cell 的x 改变
    for (int i = 0; i < self.resoultDic.allKeys.count; i++) {
        NSString *key = self.resoultDic.allKeys[i];
        NSArray *valueArray = self.resoultDic[key];
        for (int j = 0; j < valueArray.count; j++) {
            UICollectionViewLayoutAttributes *JLayoutAttributes = valueArray[j];
            if (j == 0 ) {
                CGRect frame = JLayoutAttributes.frame;
                frame.origin.x = 20;//  修改成你想要的 值
                JLayoutAttributes.frame = frame;
            }else{
                UICollectionViewLayoutAttributes *prevLayoutAttributes = valueArray[j - 1];
                // 当cell 的个数大于2个的时候
                NSInteger origin = CGRectGetMaxX(prevLayoutAttributes.frame);
                CGRect frame = JLayoutAttributes.frame;
                frame.origin.x = origin + maximumSpacing;
                JLayoutAttributes.frame = frame;
            }
        }
    }
    return attributes;
}

@end
// 使用

 SelectCollectionLayout *layout = [[SelectCollectionLayout alloc]init];
 _mainCollection = [[UICollectionView alloc]initWithFrame:elf.view.bounds     collectionViewLayout:layout];
 _mainCollection.backgroundColor = [UIColor whiteColor];
 _mainCollection.delegate = self;
 _mainCollection.dataSource = self;
 [self.view addSubview:_mainCollection];

// 剩下的你懂得

转载于:https://my.oschina.net/iOScoderZhao/blog/869887

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值