objective-c - NSCollectionView 标头显示
问题描述
我有一个NSCollectionView
包含 3 个部分的部分,其中包含来自调色板的颜色。我希望每个部分都有一个带有该部分名称的标题视图。乍一看,它似乎有效。在启动时,它看起来像这样:
但是,一旦我调整窗口大小,标题文本最终会在标题之外绘制(并且也在其中,不知何故!):
为了在集合视图中有标题,我有一个标题视图类,名为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
我是否错误地实施了上述任何方法?有什么方法我需要实现而我没有吗?
解决方案
的返回值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;
}
推荐阅读
- python - 在 Pythonanywhere 文件中导入多个模块时遇到问题
- python - 将数据从 Yelp API 导出到 csv 文件
- java - 运行应用程序时似乎无法输入任何整数值?它只显示结果“c级”
- python - 如何使用 numpy 在二维数组上执行高斯池化
- python - SVG 加载到 Frame
- mysql - Docker Compose:Percona XtraDB 集群引导
- javascript - 使用复合外键时使用 Sequelize 的“包含”问题
- dafny - Dafny 循环不变量可能不成立
- swift - SwiftUI 如何将新选择的图像更新到单元格
- macos - 将低延迟音频从一个 CoreAudio 设备路由到另一个