简体   繁体   English

使用节的UICollectionView在单元格之间留下间隙

[英]UICollectionView using sections leaves a gap between cells

I'm using a UICollectionView to show several sections of data. 我正在使用UICollectionView来显示几个数据部分。 These sections have a fixed number of items. 这些部分具有固定数量的项目。 I want all the items to show in a continuous grid. 我希望所有项目都以连续网格显示。

Right now I accomplish this in horizontal orientation: 现在我以水平方向完成此任务: 在此输入图像描述

But in vertical this leaves a big gap: 但在纵向上,这留下了很大的差距: 在此输入图像描述

I want to solve this gap between sections because it's ugly and it doesn't belong there. 我想解决这些部分之间的差距,因为它很丑陋并且不属于那里。 I'd be happy to use a custom FlowLayout, but I can't find a tutorial that points me in the right direction (I've found several, but none of them really touch this problem specifically.) 我很乐意使用自定义的FlowLayout,但我找不到一个能指向正确方向的教程(我发现了几个,但没有一个真正触及这个问题。)

Can anybody help me solve this problem, or at least point me in the right direction? 任何人都可以帮我解决这个问题,或者至少指出我正确的方向?

PS: I've implemented sections because I'm loading the data on the fly. PS:我已经实现了部分,因为我正在加载数据。 Using 1 section isn't an option for me at this moment. 目前使用1部分不是我的选择。

UPDATE On request I'm adding the values used for my current FlowLayout. 更新请求我正在添加用于当前FlowLayout的值。 I'm using the standard Horizontal Flow Layout on a fullscreen (minus UINavigationBar) UICollectionView with 21 items per section. 我在全屏(减去UINavigationBar)UICollectionView上使用标准的水平流布局,每个部分有21个项目。

  • Scroll direction: Horizontal 滚动方向:水平
  • Cell size: 248, 196 细胞大小:248,196
  • Header / footer size: none 页眉/页脚大小:无
  • Min spacing for cells: 10 细胞的最小间距:10
  • Min spacing for lines: 10 线的最小间距:10
  • Section Insets: 20, 20, 10, 10 部分插图:20,20,10,10

Use like this 像这样使用

It will solve Gap problem 它将解决Gap问题

UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];

layout.minimumInteritemSpacing = 0;
layout.minimumLineSpacing = 2;

To remove those gaps you need to create custom layout which will act as layout for your collection view. 要删除这些间隙,您需要创建自定义布局,作为集合视图的布局。 This class will child class for UICollectionViewFlowLayout. 此类将为UICollectionViewFlowLayout的子类。

Then you can override below two methods and can create your own custom layout as you want. 然后,您可以覆盖以下两种方法,并可以根据需要创建自己的自定义布局。

  - (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect
  - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)path

UICollectionViewLayoutAttributes is class which will deal with cell position, frame, Zindex etc UICollectionViewLayoutAttributes是一个处理单元格位置,框架,Zindex等的类

You can also use below properties. 您还可以使用以下属性。

 collectionView:layout:minimumInteritemSpacingForSectionAtIndex:
 collectionView:layout:minimumLineSpacingForSectionAtIndex:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM