多个section自定义UICollectionViewLayout,不同宽度的item等间距

2017-09-28

- (void)prepareLayout {

    [super prepareLayout];

    self.lastPoint = CGPointZero;

    self.AttrList = [NSMutableArray array];

    NSUInteger sectionCount = [self.collectionView numberOfSections];

    for (NSInteger index = 0; index < sectionCount; index++) {

        NSInteger hotItemCount = [self.collectionView numberOfItemsInSection:index];

        if (index != sectionCount-1) {

            //最后一个区不需header

            [self.AttrList addObject:[self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:[NSIndexPath indexPathForItem:0 inSection:index]]];

        }

        for (int i =0; i <hotItemCount; i++) {

            UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:index]];

            [self.AttrList addObject:attributes];

        }

        UICollectionViewLayoutAttributes *hotLastAttributes = self.AttrList.lastObject;

        self.lastPoint = CGPointMake(0, hotLastAttributes.frame.size.height +hotLastAttributes.frame.origin.y +[self itemVerticalPadding]);

    }

}

 

- (CGSize)collectionViewContentSize {

    return CGSizeMake(SCREENWIDTH-20, self.lastPoint.y);

}

 

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {

    return [self.AttrList filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(UICollectionViewLayoutAttributes *evaluatedObject, NSDictionary *bindings) {

        return CGRectIntersectsRect(rect, [evaluatedObject frame]);

    }]];

}

 

 

- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath {

    UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:elementKind withIndexPath:indexPath];

    attributes.frame = CGRectMake(0, self.lastPoint.y, SCREENWIDTH-20, _sectionHeaderHeight);

    self.lastPoint = CGPointMake(0, self.lastPoint.y +_sectionHeaderHeight);

    return attributes;

}

 

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {

    UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];

    CGFloat itemVerticalPadding = [self itemVerticalPadding];

    CGFloat itemHorizontalPadding = [self itemHorizontalPadding];

    CGSize itemTextSize = [self.delegate itemTextSizeIndexPath:indexPath];

    CGFloat itemWidth = itemTextSize.width +staticItemHotInsets.left +staticItemHotInsets.right;

    CGFloat itemHeight = itemTextSize.height +staticItemHotInsets.top +staticItemHotInsets.bottom;

    if (self.lastPoint.x +itemHorizontalPadding +itemWidth >=SCREENWIDTH-20) {

        UICollectionViewLayoutAttributes *lastAttributes = self.AttrList.lastObject;

        self.lastPoint = CGPointMake(0, lastAttributes.frame.size.height +lastAttributes.frame.origin.y);

    }

    CGRect rect = CGRectMake(self.lastPoint.x +itemHorizontalPadding, self.lastPoint.y +itemVerticalPadding, itemWidth, itemHeight);

    attributes.frame = rect;

    self.lastPoint = CGPointMake(rect.origin.x +rect.size.width, self.lastPoint.y);

    return attributes;

}

 

- (CGFloat)itemHorizontalPadding {

    if (self.delegate && [self.delegate respondsToSelector:@selector(customItemHorizontalPadding)])

        return [self.delegate customItemHorizontalPadding];

    return 10;

}

 

- (CGFloat)itemVerticalPadding {

    if (self.delegate && [self.delegate respondsToSelector:@selector(customItemVerticalPadding)])

        return [self.delegate customItemVerticalPadding];

    return 10;

}

 

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds

{

    return NO;

}

 

转载于:https://www.cnblogs.com/idefei/p/7607201.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值