UICollectionViewFlowLayout子类导致单元格消失

时间:2013-02-17 22:10:58

标签: ios uicollectionview uicollectionviewcell uicollectionviewlayout

我试图偏移所选单元格下方所有单元格的y轴中心。我在CollectionViewFlowLayout子类中添加了一个名为extendedCell的属性,它标记了我应该在其下面覆盖其他所有内容的单元格。

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSArray *attributes = [super layoutAttributesForElementsInRect:rect];
    if(!self.extendedCell)
    {
        return attributes;
    }
    else
    {
        return [self offsetCells:attributes];
    }
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewLayoutAttributes *attribute = [super layoutAttributesForItemAtIndexPath:indexPath];
    if(!self.extendedCell)
    {
        return attribute;
    }
    else
    {
        if(![attribute.indexPath isEqual:self.extendedCell] &&
           attribute.center.y >= [super layoutAttributesForItemAtIndexPath:self.extendedCell].center.y)
        {
            CGPoint newCenter = CGPointMake(attribute.center.x,
                                            attribute.center.y + 180.f);
            attribute.center = newCenter;
        }

        return attribute;
    }
}

-(NSArray *)offsetCells:(NSArray *)layoutAttributes
{
    for(UICollectionViewLayoutAttributes *attribute in layoutAttributes)
    {
        if(![attribute.indexPath isEqual:self.extendedCell] &&
           attribute.center.y >= [super layoutAttributesForItemAtIndexPath:self.extendedCell].center.y)
        {
            CGPoint newCenter = CGPointMake(attribute.center.x,
                                            attribute.center.y + 180.0f);
            attribute.center = newCenter;
        }
    }

    return layoutAttributes;
}

事实证明,路上发生了一些不好的事情,因为底部的细胞消失了。我有一种感觉,这与单元格在UICollectionView内容大小之外有关,但在生成布局时设置大小并没有帮助。任何想法如何解决这种消失?

3 个答案:

答案 0 :(得分:1)

好的结果我发现了这个错误。似乎覆盖 - (CGSize)collectionViewContentSize有帮助。如果任何单元格位于集合视图的内容大小之外,它们就会消失。由于在任何布局属性调用之前将内容大小设置为良好,并且不允许将单元格放置在它之外,因此集合视图就可以删除它们。我认为内容大小是基于设置后的单元属性,但实际情况并非如此。

-(CGSize)collectionViewContentSize
{
    if(self.extendedCell)
    {
        CGSize size = [super collectionViewContentSize];
        return CGSizeMake(size.width, size.height + 180);
    }
    else
    {
        return [super collectionViewContentSize];
    }
}

答案 1 :(得分:1)

通过将大细胞分解成细胞来解决它,并将它们与视图连接起来。非常hacky,但iOS7有望帮助。

答案 2 :(得分:0)

检查返回的单元格大小是否有一个边框大小等于0。

在我的情况下,消失的原因是sizeForItem返回{320,0}

Why UICollectionView with UICollectionViewFlowLayout not show cells, but ask for size?

为了从笔尖(使用自动布局)获得正确的大小视图,我使用:

+ (CGSize)sizeForViewFromNib:(NSString *)nibName width:(CGFloat)width userData:(id)userData {
        UIView *view = viewFromNib(nibName, nil);
    [view configForUserData:userData];
    CGSize size = [view systemLayoutSizeFittingSize:CGSizeMake(width, SOME_MIN_SIZE) withHorizontalFittingPriority:UILayoutPriorityRequired verticalFittingPriority:UILayoutPriorityFittingSizeLevel];
    return CGSizeMake(width, size.height);
}
相关问题