我正在实现一个自定义的流布局。它有两种主要的重写方法来确定单元格的位置:layoutAttributesForElementsInRect和layoutAttributesForItemAtIndexPath。
在我的代码中,调用了layoutAttributesForElementsInRect,但没有调用layoutAttributesForItemAtIndexPath。是什么决定了调用哪个?layoutAttributesForItemAtIndexPath在哪里被调用?
发布于 2014-08-29 18:53:14
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
}https://stackoverflow.com/questions/24490841
复制相似问题