gpt4 book ai didi

ios - 在应用于单元格之前未设置自定义 Collection View 布局属性

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:59:44 25 4
gpt4 key购买 nike

我正在编写自己的自定义布局 Collection View 布局(对流布局进行较小的自定义),为了简化一些事情,我也尝试将 UICollectionViewFlowLayoutAttributes 子类化。一切都运行良好,但是当我尝试在我的自定义单元格中应用我的布局属性时,每个属性都是 nil。

单元格(属性在这里都是零):

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

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

}

自定义流布局(属性被完美应用!!!):

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

我做错了什么或遗漏了什么吗? :).

注意:我在 Storyboard中创建了所有单元格,并在 Storyboard中设置了自定义布局。

谢谢你们。

最佳答案

解决了问题...显然 UICollectionViewLayoutAttributes 必须符合 NSCopying 并实现 copyWithZone:。完成后一切正常。

关于ios - 在应用于单元格之前未设置自定义 Collection View 布局属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25997054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com