Подтвердить что ты не робот

UICollectionViewLayout layoutAttributesForElementsInRect и layoutAttributesForItemAtIndexPath

Я реализую настраиваемый макет потока. Он имеет 2 основных метода для переопределения для определения размещения ячеек: layoutAttributesForElementsInRect и layoutAttributesForItemAtIndexPath.

В моем коде вызывается layoutAttributesForElementsInRect, но layoutAttributesForItemAtIndexPath нет. Что определяет, что вызвано? Где вызывается layoutAttributesForItemAtIndexPath?

4b9b3361

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