Ответ 1
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
}