.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 == ) {
_pageWidth = [UIScreen mainScreen].bounds.size.width;
}
self.itemSize = self.size;
self.minimumLineSpacing = self.rowSpacing;
self.minimumInteritemSpacing = self.columnSpacing; NSUInteger count = [self.collectionView numberOfItemsInSection:];
for (NSUInteger i = ; i<count; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:];
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, );
} @end

UICollectionView 自定义横向排版的更多相关文章

  1. 用NSCalendar和UICollectionView自定义日历,并实现签到显示

    前一段时间因为工作需要实现了一个可以签到的日历,来记录一下实现的思路 效果如图:   这里的基本需求是: 1,显示用户某个月的签到情况,已经签到的日子打个圈,没有签到且在某个时间范围内的可以签到,其他 ...

  2. iOS开发之窥探UICollectionViewController(三) --使用UICollectionView自定义瀑布流

    上篇博客的实例是自带的UICollectionViewDelegateFlowLayout布局基础上来做的Demo, 详情请看<iOS开发之窥探UICollectionViewControlle ...

  3. UICollectionView 自定义组头组尾的XIB方法

    UICollectionView的加载方式和Tableview很像,基本上加载的方法都差不多,尤其是它的数据源的方法和代理方法基本上类似,只不过是名字上有点细微的差别而已.这里面不赘述. 1. UIC ...

  4. UICollectionView 图片横向滑动居中偏移量的解决

    1.在使用UICollectionView 来显示横向滑动图片的时候,cell与cell之间有间隙,每次滑动后cell都会向左偏移,在使用过这两个方法才解决每次向左偏移的部分. 2.使用方法前不要开启 ...

  5. UICollectionView自定义cell布局layout

    写一个类继承UICollectionViewLayout,这个类需要提供一个数组来标识各个cell的属性信息,包括位置,size大小,返回一个UICollectionViewLayoutAttribu ...

  6. ProgressBar学习笔记,自定义横向进度条的样式(包含ActionBar上面的进度条)

     点显示进度条后→   android:max="100" 进度条的最大值 android:progress  进度条已经完成的进度值 android:progressDrawab ...

  7. 一个UICollectionView自定义layout的实现

      #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @ ...

  8. swift - uicollectionView自定义流水布局

    TYWaterFallLayout 不规则流水布局 - swift3.0 配图 使用方法 //创建layout let layout = TYWaterFallLayout() layout.sect ...

  9. 【android】简易实现横向的ListView

    众所周知,android里面的ListView是竖着的. 如果想要横向的话需要自定义一下ListView. CSDN上面有个人描述了一下一个国外大神的自定义横向ListVIew  请点击 --> ...

随机推荐

  1. json日期格式话

    // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占 ...

  2. 执行“hdfs dfs -ls”时报ConnectException

    原因可能是指定的端口号不对,该端口号由hdfs-site.xml中的属性"dfs.namenode.rpc-address"指定,即为NameNode的RPC服务端口号. 文件上传 ...

  3. JavaBugCharSequence

    目录 文章背景 目录 问题分析 问题解决 说明 参考文章 版本记录 文章背景 新建一个项目时候,不知道为什么,代码出现java.lang.CharSequence cannot be resolved ...

  4. 设计模式17:Iterator 迭代器模式(行为型模式)

    Iterator 迭代器模式(行为型模式) 动机(Motivation) 在软件构建过程中,集合对象内部结构常常变化各异.但对于这些集合对象,我们希望在不暴露其内部结构的同时,可以让外部客户代码可以透 ...

  5. XJOI 3605 考完吃糖(DAG图dfs)

    题目描述: 期末考试考完了,分数也出来了,大家准备吃糖庆祝一下,为了鼓励同学们下学期能取得更好的成绩,司马红豆同学让n个同学站成一排,如果某个同学的分数比相邻的一个同学要高,那么他得到的糖果就会比这个 ...

  6. 卡在Initializing Spring root WebApplicationContext

    1,多数情况下是连接数据库时出现问题, 2,如果使用mybatis 请查看 xml映射文件是否和对应的java 的dao文件名字相同,或者<mapper namespace="com. ...

  7. windows7下安装MySQL-5.6.34

    下载安装包"mysql-5.6.34-winx64.zip",放到某个文件夹中,解压安装包,重命名文件夹名为"mysql-5.6.34"   修改"m ...

  8. 查看SSD寿命

    查看SSD寿命 起初买mac book pro的时候挺担心SSD使用寿命的,过保了后,还搞了个移动硬盘,尽可能的把编译什么的都移动到移动硬盘上进行,实际上这样做都是没有必要的. 安装软件smartct ...

  9. python读写Excel文件(xlrd、xlwr)

    一.首先需要安装第三方库:pip install xlrd 1.打开Excel文件,由于写入时需要copy,所以这里加上保留原格式参数:formatting_info=True excel_file ...

  10. MicroPython (一)点亮我的Led

    工具 : putty F429Discovery 开发板 Notepad++ 注意:不知道为什么 其他的终端工具有问题,推荐 putty 基本没有发现问题 putty 实时调试 使用putty 打开U ...