.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. spring4-3-AOP-基于配置文件

    1.建立业务类和切面类 2.在配置文件中配置bean 引入命名空间:

  2. Jenkins持续集成构建

    配置Sonar.Jenkins进行持续审查 http://go2live.cn/archives/38261.html Jenkins iOS 项目持续集成 http://go2live.cn/arc ...

  3. code1043 方格取数

    第一次走用dfs枚举每种情况,第二次走用dp求剩下的最大值 设一个点集q用来保存有价值的点,排序,在最后加一个终点:x=m+1,y=m+1,v=0  //m是矩阵长宽 因为v=0的点是没有意义的,所以 ...

  4. 9个使用前必须再三小心的 Linux 命令-乾颐堂

    Linux shell/terminal 命令非常强大,即使一个简单的命令就可能导致文件夹.文件或者路径文件夹等被删除. 在一些情况下,Linux 甚至不会询问你而直接执行命令,导致你丢失各种数据信息 ...

  5. [Groovy]获取当前活动的Environment,获取response中节点的name和节点的value

    import com.eviware.soapui.support.GroovyUtils import com.eviware.soapui.support.XmlHolder import org ...

  6. excel判断新增使用字典作为页面级数据库,减少io

    前提:该机构录入的都是和该机构有关的数据,机构下的funmental(idcard唯一)和creditinfo(funmentalid唯一)不能重复所以推出以下结论:1.根据userid(机构)=&g ...

  7. 工作流系统webservice服务

    http://blog.csdn.net/zhengzhb/article/details/7477616

  8. CodeForces 499D. Name That Tune(概率dp)

    It turns out that you are a great fan of rock band AC/PE. Peter learned that and started the followi ...

  9. unigui不是单个网页相应的链接,而是整体Web Application,如何把webApp的子功能映射到微信公众号菜单?

    只需要用UniApplication.Parameters.Values[‘xxx’]读取url的参数然后调用就可以 例如:要打开公众号菜单的取样送检指南查询模块,在自定义菜单设定:http://ww ...

  10. 在Android模拟器里安装apk

    [原文]http://Android.tgbus.com/android/tutorial/201104/349532.shtml 1.运行SDK Manager,选择模拟器,并运行模拟器. 2.将需 ...