NSCollectionView header 显示

NSCollectionView header display

我有一个 NSCollectionView,它有 3 个部分,其中包含调色板中的颜色。我希望每个部分都有一个带有该部分名称的 header 视图。乍一看,它似乎有效。启动时,它看起来像这样:

但是,一旦我调整了 window 的大小,header 文本最终绘制在 header 之外(不知何故也在其中!):

为了在 collection 视图中包含 header,我有一个名为 CollectionHeaderView 的 header 视图 class。我将此 class 注册为补充视图:

    [_collection registerClass:[CollectionHeaderView class]
    forSupplementaryViewOfKind:@"UICollectionElementKindSectionHeader"
                withIdentifier:kSupplementalView];

在 collection 视图的委托中,我实现了 return 补充元素视图的方法:

- (NSView *)collectionView:(NSCollectionView *)collectionView
viewForSupplementaryElementOfKind:(NSCollectionViewSupplementaryElementKind)kind
               atIndexPath:(NSIndexPath *)indexPath {
    NSView* result = [collectionView makeSupplementaryViewOfKind:kind
                                                  withIdentifier:kSupplementalView
                                                    forIndexPath:indexPath];

    if ([kind isEqualToString:@"UICollectionElementKindSectionHeader"]) {
        NSTextField* textView = ((CollectionHeaderView*)result).textField;
        switch (indexPath.section) {
            case 0:
                [textView setStringValue:@"Bright Colors"];
                break;

            case 1:
                [textView setStringValue:@"Pastel Colors"];
                break;

            case 2:
                [textView setStringValue:@"Designer Colors"];
                break;
        }

        return textView;
    }

    return nil;
}

此外,如果我调整 window 的大小,header 视图不会移动。 collection 元素似乎在 header 周围流动,将内容列在错误的部分下。而且,正如您在上面的第二张图片中看到的,加宽 window 最终会在 header 的右边缘和 collection 视图的右边缘之间留下间隙。

CollectionHeaderView class 很简单。它只是创建一个文本字段并绘制背景和文本字段:

@implementation CollectionHeaderView

- (instancetype)initWithFrame:(NSRect)frameRect {
    self = [super initWithFrame:frameRect];

    if (self != nil) {
        NSRect textFrame = frameRect;
        _textField = [[NSTextField alloc] initWithFrame:textFrame];
        _textField.editable = NO;
        _textField.bordered = NO;
        _textField.font = [NSFont fontWithName:@"Helvetica-Bold" size:14.0];
        _textField.backgroundColor = [NSColor colorWithSRGBRed:0.0 green:0.0 blue:0.0 alpha:0.0];
        [self addSubview:_textField];

        self.identifier = kSupplementalView;
    }

    return self;
}

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];

    // Drawing code here.
    NSBezierPath*   fillPath    = [NSBezierPath bezierPath];
    [fillPath appendBezierPathWithRect:dirtyRect];
    [[NSColor lightGrayColor] setFill];
    [fillPath fill];

    [_textField drawRect:dirtyRect];
}

@end

我是否错误地实施了上述任何方法?是否有任何我需要实现但我没有实现的方法?

collectionView:viewForSupplementaryElementOfKind:atIndexPath:的return值是补充观点result

- (NSView *)collectionView:(NSCollectionView *)collectionView
viewForSupplementaryElementOfKind:(NSCollectionViewSupplementaryElementKind)kind
               atIndexPath:(NSIndexPath *)indexPath {
    NSView* result = [collectionView makeSupplementaryViewOfKind:kind
                                                  withIdentifier:kSupplementalView
                                                    forIndexPath:indexPath];

    if ([kind isEqual:NSCollectionElementKindSectionHeader]) {
        NSTextField* textView = ((CollectionHeaderView*)result).textField;
        switch (indexPath.section) {
            case 0:
                [textView setStringValue:@"Bright Colors"];
                break;

            case 1:
                [textView setStringValue:@"Pastel Colors"];
                break;

            case 2:
                [textView setStringValue:@"Designer Colors"];
                break;
        }

        return result;
    }

    return nil;
}