我正在实现自定义流程布局。它有两种主要方法来覆盖以确定单元格的位置:layoutAttributesForElementsInRect
and layoutAttributesForItemAtIndexPath
.
在我的代码中,layoutAttributesForElementsInRect
被称为,但是layoutAttributesForItemAtIndexPath
不是。什么决定哪个被调用?哪里有layoutAttributesForItemAtIndexPath
接到电话吗?
layoutAttributesForElementsInRect:
不一定会打电话layoutAttributesForItemAtIndexPath:
.
事实上,如果你子类化UICollectionViewFlowLayout
,流布局将准备布局并缓存结果属性。所以,当layoutAttributesForElementsInRect:
被调用,它不会询问layoutAttributesForItemAtIndexPath:
,但仅使用缓存的值。
如果您想确保布局属性始终根据您的布局进行修改,请为两者实现修饰符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
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)