Custom collection layout attributes are not set before being applied to a cell

I am writing my own custom layout view layout (fine tweaks for flow layout) and simplifying some of the things I am trying to subclass UICollectionViewFlowLayoutAttributes too. Everything works fine, but when I try to apply my layout attributes in my custom cell, every attribute is null.

Cell (all attributes are missing here):

- (void)applyLayoutAttributes:(CustomLayoutAttributes *)layoutAttributes
{
    [super applyLayoutAttributes:layoutAttributes];

    NSLog(@"layoutAttributes %@", layoutAttributes.description);

}

      

Custom Flow Layout (Attributes apply great !!!):

+ (Class)layoutAttributesClass
{
    return [CustomLayoutAttributes class];
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSArray *attributesInRect = [super layoutAttributesForElementsInRect:rect];

    [attributesInRect enumerateObjectsUsingBlock:^(CustomLayoutAttributes *layoutAttributes, NSUInteger idx, BOOL *stop) {
        if (layoutAttributes.representedElementCategory == UICollectionElementCategoryCell) {
            [self configureLayoutAttributes:layoutAttributes];
        }
    }];

    return attributesInRect;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CustomLayoutAttributes  *layoutAttributes = (CustomLayoutAttributes *)[super layoutAttributesForItemAtIndexPath:indexPath];

    if (layoutAttributes.representedElementCategory == UICollectionElementCategoryCell) {
        [self configureLayoutAttributes:layoutAttributes];
    }
    return layoutAttributes;
}

- (void)configureLayoutAttributes:(CustomLayoutAttributes *)layoutAttributes
{
    CustomCollectionView *collectionView = (CustomCollectionView *)self.collectionView;

    layoutAttributes.messageTopLabelHeight = 20.0;
    layoutAttributes.messageBottomLabelHeight = 20.0;
    layoutAttributes.messageBubbleFont = _messageBubbleFont;
}

      

Am I doing something wrong or am I missing something?:.)

Note. I created all my cells in the storyboard and set my own layout in the storyboard as well.

Thanks guys.

+3


source to share


1 answer


figured out the problem ... obviously UICollectionViewLayoutAttributes must conform to NSCopying and implement copyWithZone :. Everything worked fine after that.



+3


source







All Articles