Ios UICollectionViewFlowLayout未正确断线
我对flow layout进行了子分类,并在我的集合视图中将其设置为myIos UICollectionViewFlowLayout未正确断线,ios,uikit,uicollectionview,Ios,Uikit,Uicollectionview,我对flow layout进行了子分类,并在我的集合视图中将其设置为mycollectionViewLayout 然后我设置布局如下: - (id) init { if(self = [super init]) { self.minimumInteritemSpacing = 11.0; self.scrollDirection = UICollectionViewScrollDirectionVertical; self.item
collectionViewLayout
然后我设置布局如下:
- (id) init
{
if(self = [super init])
{
self.minimumInteritemSpacing = 11.0;
self.scrollDirection = UICollectionViewScrollDirectionVertical;
self.itemSize = CGSizeMake(64.0,32.0);
self.minimumLineSpacing = 10.0;
self.sectionInset = UIEdgeInsetsMake(11.0,95.0,11.0,11.0);
return self;
}
return nil;
}
95+64+11+64+11+64+11=320-我检查过了。(这是一个左插图、3个单元格、2个空格和一个右插图)
我有1个部分、72个项目和用于索引路径函数的单元格:
- (UICollectionViewCell*) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifierForCustomCell forIndexPath:indexPath];
cell.mainLabel.text = [@(indexPath.item) stringValue];
return cell;
}
输出如下:
如您所见,每行只有2个单元格。此外,它不是每3个单元格显示一次
我看了一下flow layout的布局功能产生了什么
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
给。。。
这是layouttributes[NSArray]
中的前3个布局属性,如您所见,它决定只将前2个和第4个项目视为对rect可见(320 x 568)。这一定是集合视图布局中的错误,不是吗?集合视图充满屏幕,宽度为320。因此,由于这是一个换行布局,任何项目都不应该离开屏幕
删除边缘插入后:
它按预期工作。但我希望有部分插图,因为我需要添加一些装饰视图,这些视图需要在同一个滚动视图中,而我需要一个比右插图大得多的左插图。苹果的UICollectionViewFlowLayout中有一个bug,其中一些单元格显示在边界之外。 这些资源将帮助您修复它(您必须创建UICollectionViewFlowLayout的子类并重写此方法):
-(NSArray*)布局属性ForElementsInRect:(CGRect)rect{
NSArray*属性=[super-layouttributesforementsinrect:rect];
NSMutableArray*newAttributes=[NSMutableArray阵列容量:attributes.count];
用于(UICollectionViewLayoutAttributes*属性中的属性){
if((attribute.representedElementCategory!=UICollectionElementCategoryCell)|///始终包括非单元格的所有内容
((attribute.frame.origin.x+attribute.frame.size.width直到有人弄清楚发生了什么以及这是否是一个bug,我目前用来修复这个问题的方法是:
- (NSArray *)alterAttributes:(NSArray *)attributes
{
for (UICollectionViewLayoutAttributes *attribute in attributes) {
switch (attribute.indexPath.item % 3) {
case 0:
attribute.center = CGPointMake(CellX1,attribute.center.y);
break;
case 1:
attribute.center = CGPointMake(CellX2,attribute.center.y);
break;
case 2:
attribute.center = CGPointMake(CellX3,attribute.center.y);
break;
default:
break;
}
}
return attributes;
}
手动更改layoutAttributesForElementsInRect中的x坐标-这不是一个令人满意的解决方案。不幸的是,我已经尝试过这个方法。如果我读得正确,这是从绘图中删除“额外”部分屏幕外项目。我的问题是,该函数没有包括所有应该在scr上的项目eenNo,再试一次。这将解决您的问题。我遇到了完全相同的问题。问题源于在某些情况下忽略其计算中的插入的布局。然后它为单个单元格返回2(!)个布局属性对象,其中一个无效。此代码修复了该问题。我的属性数组打印输出(如上)通过将断点放在该函数的“return newAttributes”行获得。attributes中有28项,newAttributes中有28项。如果您想一想,我的问题是[super layout…]正在跳过某些索引路径(应该显示)中的项;一个实际过滤属性数组的函数如何将这些跳过的索引路径添加回?它的工作原理与预期一样。它每行显示4个项目,因为4个项目=256,3个空格=33和320>(256+33)如果sectionInset为(11,0,11,0),我附加了一个屏幕截图
- (NSArray *)alterAttributes:(NSArray *)attributes
{
for (UICollectionViewLayoutAttributes *attribute in attributes) {
switch (attribute.indexPath.item % 3) {
case 0:
attribute.center = CGPointMake(CellX1,attribute.center.y);
break;
case 1:
attribute.center = CGPointMake(CellX2,attribute.center.y);
break;
case 2:
attribute.center = CGPointMake(CellX3,attribute.center.y);
break;
default:
break;
}
}
return attributes;
}