NSCollectionView标头显示

问题描述 投票:0回答:1

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

A window showing various colors grouped by type, such as Bright Colors, and Pastel Colors

但是,当我调整窗口大小时,标题文本最终会在标题之外(以及内部,以某种方式!)绘制:]

The color palette again with the header text drawn incorrectly.

为了在集合视图中具有标题,我有一个名为CollectionHeaderView的标题视图类。我将此课程注册为补充视图:

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

在集合视图的委托中,我实现了用于为补充元素返回视图的方法:

- (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;
}

此外,如果我调整窗口大小,则标题视图不会移动。集合元素似乎在标头周围流动,从而在错误的部分下列出了所有内容。而且,如您在上面的第二张图片中所看到的,加宽窗口最终导致在页眉的右边缘和集合视图的右边缘之间留出间隙。

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

@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

我是否错误地实现了上述任何一种方法?有什么我需要实现的方法吗?

objective-c macos nscollectionview
1个回答
0
投票

collectionView:viewForSupplementaryElementOfKind:atIndexPath:的返回值是补充视图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;
}
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.