gpt4 book ai didi

ios - UICollectionViewLayout layoutAttributesForElementsInRect 和 layoutAttributesForItemAtIndexPath

转载 作者:技术小花猫 更新时间:2023-10-29 10:45:21 25 4
gpt4 key购买 nike

我正在实现自定义流布局。它有 2 个主要的重写方法来确定单元格的位置:layoutAttributesForElementsInRectlayoutAttributesForItemAtIndexPath

在我的代码中,调用了 layoutAttributesForElementsInRect,但未调用 layoutAttributesForItemAtIndexPath。什么决定了哪个被调用? layoutAttributesForItemAtIndexPath 在哪里被调用?

最佳答案

layoutAttributesForElementsInRect:不一定调用 layoutAttributesForItemAtIndexPath: .

事实上,如果你继承UICollectionViewFlowLayout ,流式布局将准备布局并缓存生成的属性。所以,当layoutAttributesForElementsInRect:被调用,它不会询问layoutAttributesForItemAtIndexPath: ,但只使用缓存的值。

如果您想确保布局属性始终根据您的布局进行修改,请为 layoutAttributesForElementsInRect: 实现一个修饰符和 layoutAttributesForItemAtIndexPath: :

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSArray *attributesInRect = [super layoutAttributesForElementsInRect:rect];
for (UICollectionViewLayoutAttributes *cellAttributes in attributesInRect) {
[self modifyLayoutAttributes:cellAttributes];
}
return attributesInRect;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewLayoutAttributes *attributes = [super layoutAttributesForItemAtIndexPath:indexPath];
[self modifyLayoutAttributes:attributes];
return attributes;
}

- (void)modifyLayoutAttributes:(UICollectionViewLayoutAttributes *)attributes
{
// Adjust the standard properties size, center, transform etc.
// Or subclass UICollectionViewLayoutAttributes and add additional attributes.
// Note, that a subclass will require you to override copyWithZone and isEqual.
// And you'll need to tell your layout to use your subclass in +(Class)layoutAttributesClass
}

关于ios - UICollectionViewLayout layoutAttributesForElementsInRect 和 layoutAttributesForItemAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24490841/

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