- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
我正在实现自定义流布局。它有 2 个主要的重写方法来确定单元格的位置:layoutAttributesForElementsInRect
和 layoutAttributesForItemAtIndexPath
。
在我的代码中,调用了 layoutAttributesForElementsInRect
,但未调用 layoutAttributesForItemAtIndexPath
。什么决定了哪个被调用? layoutAttributesForItemAtIndexPath
在哪里被调用?
最佳答案
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
}
关于ios - UICollectionViewLayout layoutAttributesForElementsInRect 和 layoutAttributesForItemAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24490841/
我目前正在研究新的 UICollectionView 的一些演示项目。大多数此类演示都覆盖了子类中的 layoutAttributesForItemAtIndexPath。但是在其中一个示例中从未调用
我目前正在研究新的 UICollectionView 的一些演示项目。大多数此类演示都覆盖了子类中的 layoutAttributesForItemAtIndexPath。但是在其中一个示例中从未调用
几天来一直试图理解这次崩溃。 对于某些用户(随机,操作系统 10.14/10.14.1)应用程序在第一次加载时崩溃。 Crashed: com.apple.main-thread EXC_BAD_AC
我正在对 UICollectionViewLayout 进行子类化,并且永远不会调用 layoutAttributesForItemAtIndexPath: 方法。 我也知道要覆盖 layoutAtt
我正在实现自定义流布局。它有 2 个主要的重写方法来确定单元格的位置:layoutAttributesForElementsInRect 和 layoutAttributesForItemAtInde
完整的断言信息是: Assertion failure in -[UICollectionViewData layoutAttributesForItemAtIndexPath:], /SourceC
我有一个 Collection View ,我必须动态更改 Collection View 的布局。在设置新布局时,应用程序崩溃并显示日志, 2016-02-15 13:47:31.663 Photo
我有一个带有自定义 UICollectionLayout 的 UICollectionView。一切正常,直到我尝试插入一行…… 然后我收到以下错误,似乎无法弄清楚如何解决它。 *** Termina
我正在使用像这样的自定义 UICollectionViewLayout : override func prepareLayout() { cache.removeAll(keepCa
我是一名优秀的程序员,十分优秀!