gpt4 book ai didi

ios - 自定义标题在 UICollectionView 布局中的位置会导致 NSInternalInconsistencyException 错误

转载 作者:技术小花猫 更新时间:2023-10-29 10:59:28 24 4
gpt4 key购买 nike

我正在尝试使用子类 UICollectionViewFlowLayout 类自定义 UICollectionView 中 header 的位置(松散地基于显示的堆叠 header 的代码 enter link description here ).

作为最小测试,假设我只想向所有 header 的位置添加一个固定偏移量:

  • 我将所有 header 添加到 layoutAttributesForElementsInRect 返回的数组中,以便始终处理所有 header (这可能是问题的原因,我不确定)
  • 然后我通过在 layoutAttributesForSupplementaryViewOfKind
  • 中添加固定偏移量来更新每个 header

本文末尾包含完整的实现。

(顺便说一下,我知道添加所有标题,包括那些在矩形之外的标题,严格来说在第一步中并不是必需的,但这是我想要在位置进行更复杂自定义的简化示例导致所有标题显示在绘制矩形中。)

但是,当我运行代码时,我得到以下 NSInternalInconsistencyException:

2014-01-15 00:41:50.130 CollectionStackedHeaders[60777:70b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: 'layout attributes for supplementary item at index path (<NSIndexPath: 0x8a7db90> {length = 2, path = 0 - 0})
changed from <UICollectionViewLayoutAttributes: 0x8a7f8b0> index path: (<NSIndexPath: 0x8a7d9c0> {length = 2, path = 0 - 0}); element kind: (UICollectionElementKindSectionHeader); frame = (0 0; 320 50);
to <UICollectionViewLayoutAttributes: 0x8a7fb80> index path: (<NSIndexPath: 0x8a7db90> {length = 2, path = 0 - 0}); element kind: (UICollectionElementKindSectionHeader); frame = (0 50; 320 50); zIndex = 1024;
without invalidating the layout'

看来这是属性更新引起的,好像我注释掉下面两行就可以了:

attributes.zIndex = 1024;
attributes.frame = frame;

是什么导致了这个错误,我该怎么做才能启动并运行我的简单示例?

这是这个简单示例的完整类实现:

@implementation myStackedHeaderFlowLayout

- (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect {
// Call super to get elements
NSMutableArray* answer = [[super layoutAttributesForElementsInRect:rect] mutableCopy];

// As a test, always add first header to the answer array
NSArray* indexes = [NSArray arrayWithObjects: [NSNumber numberWithInt:0], nil];
for (NSNumber* sectionNumber in indexes) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:[sectionNumber integerValue]];
UICollectionViewLayoutAttributes* layoutAttributes = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:indexPath];
if (layoutAttributes) {
[answer removeObject:layoutAttributes]; // remove if already present
[answer addObject:layoutAttributes];
}
}

return answer;
}

- (UICollectionViewLayoutAttributes*)layoutAttributesForSupplementaryViewOfKind:(NSString*)kind atIndexPath:(NSIndexPath*)indexPath {
// Call super to get base attributes
UICollectionViewLayoutAttributes* attributes = [super layoutAttributesForSupplementaryViewOfKind:kind atIndexPath:indexPath];

if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
CGRect frame = attributes.frame;
frame.origin.y += 50;
// Update attributes position here - causes the problem
attributes.zIndex = 1024;
attributes.frame = frame;
}

return attributes;
}

- (UICollectionViewLayoutAttributes*)initialLayoutAttributesForAppearingSupplementaryElementOfKind:(NSString*)kind atIndexPath:(NSIndexPath*)indexPath {
UICollectionViewLayoutAttributes* attributes = [self layoutAttributesForSupplementaryViewOfKind:kind atIndexPath:indexPath];
return attributes;
}

- (UICollectionViewLayoutAttributes*)finalLayoutAttributesForDisappearingSupplementaryElementOfKind:(NSString*)kind atIndexPath:(NSIndexPath*)indexPath {
UICollectionViewLayoutAttributes* attributes = [self layoutAttributesForSupplementaryViewOfKind:kind atIndexPath:indexPath];
return attributes;
}

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBound {
return YES;
}

@end

最佳答案

layout attributes for supplementary item at index path (<NSIndexPath>) 
changed from <UICollectionViewLayoutAttributes>
to <UICollectionViewLayoutAttributes>
without invalidating the layout

根据我的经验,当从 layoutAttributesForElementsInRect: 返回的数组包含两个 UICollectionViewLayoutAttributes 对象时,会抛出上述描述的 NSInternalInconsistencyException strong>相同的索引路径和(补充)元素类别。

关于ios - 自定义标题在 UICollectionView 布局中的位置会导致 NSInternalInconsistencyException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21127091/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com