• UICollectionView 自定义横向排版


    .h

    #import <UIKit/UIKit.h>
    
    @interface JHCollectionViewFlowLayout : UICollectionViewFlowLayout
    ///一页展示行数
    @property (nonatomic, assign) NSInteger row;
    ///一页展示列数
    @property (nonatomic, assign) NSInteger column;
    ///行间距
    @property (nonatomic, assign) CGFloat rowSpacing;
    ///列间距
    @property (nonatomic, assign) CGFloat columnSpacing;
    ///item大小
    @property (nonatomic, assign) CGSize size;
    ///一页的宽度
    @property (nonatomic, assign) CGFloat pageWidth;
    
    @end

    .m

    #import "JHCollectionViewFlowLayout.h"
    
    @interface JHCollectionViewFlowLayout()
    @property (strong,  nonatomic) NSMutableArray   *attributesArray;
    @end
    
    @implementation JHCollectionViewFlowLayout
    
    - (void)prepareLayout
    {
        [super prepareLayout];
        
        _attributesArray = @[].mutableCopy;
        if (_pageWidth == 0) {
            _pageWidth = [UIScreen mainScreen].bounds.size.width;
        }
        self.itemSize = self.size;
        self.minimumLineSpacing = self.rowSpacing;
        self.minimumInteritemSpacing = self.columnSpacing;
        
        NSUInteger count = [self.collectionView numberOfItemsInSection:0];
        for (NSUInteger i = 0; i<count; i++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
            UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:indexPath];
            [_attributesArray addObject:attributes];
        }
    }
    
    //计算每个item的frame
    - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{
        UICollectionViewLayoutAttributes *attribute = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        
        NSInteger index = indexPath.item;
        
        NSInteger page = index / (_row * _column);
        
        // % 运算 确定 x 是 0,1,2 ... _column-1
        CGFloat x = index % _column * (_size.width + _columnSpacing) + page * _pageWidth;
        // / 运算 确定 y 是 在哪行(一行有 column 个), % 确定在 0,1,2 ... _row-1 行内的哪行
        CGFloat y = (index / _column % _row) * (_size.height + _rowSpacing);
        
        attribute.frame = CGRectMake(x, y, _size.width, _size.height);
        
        return attribute;
        
    }
    
    //返回所有item的frame
    - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{
        return _attributesArray;
    }
    
    //返回总的可见尺寸
    //避免一页未排满,滑动显示不全
    - (CGSize)collectionViewContentSize{
        int width = (int)ceil(_attributesArray.count/(_row * _column * 1.0)) * _pageWidth;
        return CGSizeMake(width, 0);
    }
    
    @end
  • 相关阅读:
    3、excel—如何把相同年月日的中相同的数据相加
    Linux_plsql通过跳板的ssh隧道访问oracle1521(转)
    微信小程序-歌词滚动实现
    微信小程序-RSA 加解密
    如何给开源仓库贡献代码
    linux开发调试常用命令-gdb
    vector 内存分配和释放
    Ubuntu上SmartGit三十天试用到期解决办法
    Django-325版本创建时候需要手动设置的内容
    Django 博客园练习--待完成
  • 原文地址:https://www.cnblogs.com/daxueshan/p/10069156.html
Copyright © 2020-2023  润新知