UICollectionViewFlowLayout subclass makes the cell disappear
I am trying to offset the y-center of all the cells below the selected cell. I added a property to a subclass CollectionViewFlowLayout
called extendedCell
that marks the cell below which I have to 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;
}
It turns out that something bad is happening along the way as the cells below disappear. I have a feeling this is because the cells are outside the content size UICollectionView
, but adjusting the size when creating the layout doesn't help. Any ideas how to fix this disappearing?
source to share
OK, it turns out that I found a bug. It seems that overriding - (CGSize) collectionViewContentSize helps. If any cells are outside the size of the contents of the collection, they simply disappear. Since the content size is tuned to the advantage until any of the layout attributes are called and the cells are placed outside of it, viewing the collection will just get rid of them. I thought the size of the content was based on the attributes of the cells after they were 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];
}
}
source to share
Check if any returned cell size has one border size equal to 0.
In my case, the reason was sizeForItem returned {320,0}
Why is UICollectionView with UICollectionViewFlowLayout not showing cells but asking for size?
And to view the size correctly from the nib (using the self-timer) 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);
}
source to share