首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UICollectionViewLayout layoutAttributesForElementsInRect和layoutAttributesForItemAtIndexPath

UICollectionViewLayout layoutAttributesForElementsInRect和layoutAttributesForItemAtIndexPath
EN

Stack Overflow用户
提问于 2014-06-30 21:05:33
回答 1查看 13.8K关注 0票数 11

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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-29 18:53:14

layoutAttributesForElementsInRect:不一定会调用layoutAttributesForItemAtIndexPath:

实际上,如果将UICollectionViewFlowLayout子类化,流布局将准备布局并缓存结果属性。因此,当调用layoutAttributesForElementsInRect:时,它不会询问layoutAttributesForItemAtIndexPath:,而只是使用缓存的值。

如果要确保始终根据您的布局修改布局属性,请同时为layoutAttributesForElementsInRect:layoutAttributesForItemAtIndexPath:实现修饰符

代码语言:javascript
复制
- (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
}
票数 22
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24490841

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档