【问题标题】:UICollectionView with section headers带有部分标题的 UICollectionView
【发布时间】:2014-01-03 07:50:18
【问题描述】:

有人知道我为什么会收到这个错误

“'UICollectionViewController' 没有可见的@interface 声明 选择器'layoutAttributesForSupplementaryViewOfKind

任何帮助将不胜感激..

不确定错误来自哪里,我在有

的行上得到它
[super layoutAttributesForElementsInRect:..

    - (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect {

        NSMutableArray *answer = [[super layoutAttributesForElementsInRect:rect] mutableCopy];

        NSMutableIndexSet *missingSections = [NSMutableIndexSet indexSet];
        for (NSUInteger idx=0; idx<[answer count]; idx++) {
            UICollectionViewLayoutAttributes *layoutAttributes = answer[idx];

            if (layoutAttributes.representedElementCategory == UICollectionElementCategoryCell) {
                [missingSections addIndex:layoutAttributes.indexPath.section];  // remember that we need to layout header for this section
            }
            if ([layoutAttributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]) {
                [answer removeObjectAtIndex:idx];  // remove layout of header done by our super, we will do it right later
                idx--;
            }
        }

        // layout all headers needed for the rect using self code
        [missingSections enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:idx];
            UICollectionViewLayoutAttributes *layoutAttributes = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:indexPath];
            [answer addObject:layoutAttributes];
        }];

        return answer;
    }

    - (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
        UICollectionViewLayoutAttributes *attributes = [super layoutAttributesForSupplementaryViewOfKind:kind atIndexPath:indexPath];
        if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
            UICollectionView * const cv = self.collectionView;
            CGPoint const contentOffset = cv.contentOffset;
            CGPoint nextHeaderOrigin = CGPointMake(INFINITY, INFINITY);

            if (indexPath.section+1 < [cv numberOfSections]) {
                UICollectionViewLayoutAttributes *nextHeaderAttributes = [super layoutAttributesForSupplementaryViewOfKind:kind atIndexPath:[NSIndexPath indexPathForItem:0 inSection:indexPath.section+1]];
                nextHeaderOrigin = nextHeaderAttributes.frame.origin;
            }

            CGRect frame = attributes.frame;
            if (self.scrollDirection == UICollectionViewScrollDirectionVertical) {
                frame.origin.y = MIN(MAX(contentOffset.y, frame.origin.y), nextHeaderOrigin.y - CGRectGetHeight(frame));
            }
            else { // UICollectionViewScrollDirectionHorizontal
                frame.origin.x = MIN(MAX(contentOffset.x, frame.origin.x), nextHeaderOrigin.x - CGRectGetWidth(frame));
            }
            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;
    }

【问题讨论】:

  • @chris.. 请让我知道我在哪里引入了新错误,以便我可以更新我的知识。没有任何理由把它变成大写。那不是主要的编辑。我更新了一个句子和几个词。如果打扰到您,请见谅

标签: ios objective-c ios7 header uicollectionview


【解决方案1】:

UITableViewController 中不存在该方法。它是 UICollectionViewLayoutAttributes 上的类级别方法,因此如果您想更改它,您需要对该类进行子类化,而且它也是类级别方法,因此请使用“+”而不是“-”。

【讨论】:

  • 我在 UICollectionViewController 中使用它。我所做的是像你说的那样创建一个子类,然后将布局设置为自定义。并让班级将班级设置为我刚刚创建的班级。虽然它不起作用..单元格仅显示每行 5 个单元格,它们之间有空格
  • 您是否在 UICollectionViewLayoutAttributes 子类上覆盖了 isEquals?文档中的子类化注释指出,您必须重写它才能使其正常工作。
【解决方案2】:

所有这些方法都必须由您的 UICollectionViewLayout 的子类实现,而不是由您的控制器实现。

这些方法允许 UICollectionView 询问 UICollectionViewLayout 的子类如何定位单元格和补充视图(例如标题)。

您需要 2 个不同的类,一个 UICollectionViewLayout 的子类和一个实现 UICollectionViewDelegate 和 UICollectionViewDataSource 协议并提供数据(即单元格数、节数、实际单元格等)的控制器。

您看到的特定错误是由这一行引起的:

[self layoutAttributesForSupplementaryViewOfKind:kind atIndexPath:indexPath];

self(即UICollectionViewController)未在其.h 中定义此方法(它不应该)。

【讨论】:

    猜你喜欢
    • 2014-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多