• 多个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;
    
    }
  • 相关阅读:
    EFCore 中使用覆盖查询(ForSqlServerInclude方法)来优化查询速度
    Asp.Net Core中使用FTP读取大文件并使用SqlBulkCopy实现大批量插入SQL SERVER数据库
    EFCore 2.2 报错 Data is Null. This method or property cannot be called on Null values
    在Asp.Net Core中集成Refit
    EFCore通过Include关联清单不存在时返回值为默认值的方式
    工作中常用英语单词
    参数的 in out in/out 修饰
    C# 的属性的写法和赋值
    raspberry pi 4b 常见的一些配置信息
    树莓派4B 更新wiringPi库到2.52的方法的wiringPi库2.5.2版本wiringpi-latest.deb下载
  • 原文地址:https://www.cnblogs.com/idefei/p/7607201.html
Copyright © 2020-2023  润新知