3

I am trying to offset the center on y axis, of all cells below selected cell. I added a property to CollectionViewFlowLayout subclass, called extendedCell, which marks the cell below which I should offset everything else.

- (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;
}

Turns out that something bad happens on the way, as cells at the bottom disappear. I have a feeling that this has something to do with cells being outside UICollectionView content size, but setting the size while generating layout does not help. Any ideas how to fix that disappearance?

etolstoy
  • 1,798
  • 21
  • 33
foFox
  • 1,124
  • 1
  • 14
  • 24

3 Answers3

1

OK turns out I found the bug. Seems that overriding -(CGSize)collectionViewContentSize helps. If any cells lie outside the content size of the collection view they simply disappear. As the content size is set for good before any layout attributes calls, and cells are not allowed to be placed outside of it, collection view just gets rid of them. I thought the content size is based upon cells attributes after they've been set, this is not the case.

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

Solved it by breaking big cells into minor cells an connected them with a view. Very hacky, but iOS7 will hopefully help.

netshark1000
  • 7,245
  • 9
  • 59
  • 116
0

Check if any returned size of cell have one of border size equals 0.

In my case disappear cause was sizeForItem returned {320,0}

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

And for correct size view from nib (with used autolayouts) i use:

+ (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);
}
Community
  • 1
  • 1
Dmitry Nelepov
  • 7,246
  • 8
  • 53
  • 74