带有节头的UICollectionView

时间:2014-01-03 07:50:19

标签: ios objective-c ios7 header uicollectionview

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

  

“'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;
    }

2 个答案:

答案 0 :(得分:0)

UITableViewController中不存在该方法。它是UICollectionViewLayoutAttributes上的类级方法,所以如果你想要改变它,你需要继承该类,并且它也是类级方法,所以使用'+'而不是' - '。

答案 1 :(得分:0)

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

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

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

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

[self layoutAttributesForSupplementaryViewOfKind:kind atIndexPath:indexPath];

self(即。UICollectionViewController)没有在.h中定义此方法(并且它不应该)。