UICollectionViewLayout layoutAttributesForElementsInRect和layoutAttributesForItemAtIndexPath [英] UICollectionViewLayout layoutAttributesForElementsInRect and layoutAttributesForItemAtIndexPath

查看:406
本文介绍了UICollectionViewLayout layoutAttributesForElementsInRect和layoutAttributesForItemAtIndexPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实施自定义流程布局。它有两种主要的方法来覆盖以确定单元格的位置: layoutAttributesForElementsInRect layoutAttributesForItemAtIndexPath



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

解决方案

layoutAttributesForElementsInRect:不一定调用 layoutAttributesForItemAtIndexPath:



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

如果要确保始终根据布局修改布局属性,请为 layoutAttributesForElementsInRect:和 layoutAttributesForItemAtIndexPath:

   - (NSArray *)layoutAttributesForElementsInRect :( CGRect)rect 
{
NSArray * attributesInRect = [super layoutAttributesForElementsInRect:rect];
for(UICollectionViewLayoutAttributes * attributesInRect中的cellAttributes){
[self modifyLayoutAttributes:cellAttributes];
}
返回attributesInRect;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewLayoutAttributes * attributes = [super layoutAttributesForItemAtIndexPath:indexPath];
[self modifyLayoutAttributes:attributes];
返回属性;
}

- (void)modifyLayoutAttributes:(UICollectionViewLayoutAttributes *)attributes
{
//调整标准属性大小,居中,变换等
//或者子类UICollectionViewLayoutAttributes并添加其他属性。
//注意,子类将要求您覆盖copyWithZone和isEqual。
//你需要告诉你的布局在+(Class)中使用你的子类layoutAttributesClass
}


I'm implementing a custom flow layout. It has 2 main methods for overriding to determine placement of cells: layoutAttributesForElementsInRect and layoutAttributesForItemAtIndexPath.

In my code, layoutAttributesForElementsInRect is called, but layoutAttributesForItemAtIndexPath isn't. What determines which gets called? Where does layoutAttributesForItemAtIndexPath get called?

解决方案

layoutAttributesForElementsInRect: doesn't necessarily call layoutAttributesForItemAtIndexPath:.

In fact, if you subclass UICollectionViewFlowLayout, the flow layout will prepare the layout and cache the resulting attributes. So, when layoutAttributesForElementsInRect: is called, it won't ask layoutAttributesForItemAtIndexPath:, but just uses the cached values.

If you want to ensure that the layout attributes are always modified according to your layout, implement a modifier for both layoutAttributesForElementsInRect: and 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
}

这篇关于UICollectionViewLayout layoutAttributesForElementsInRect和layoutAttributesForItemAtIndexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆