如何解决NSTableView 删除表格视图边框和单元格之间的填充/空格
我使用的是 macOS、objective-c、XCode 12 - 而不是 iOS。
我以编程方式在 NSScrollView 中创建了一个 NSTableView:
NSView* view = self.view;
// Create tableView
NSTableView* tableView = [[NSTableView alloc] initWithFrame:NSZeroRect]; // Size is set by constraints
tableView.translatesAutoresizingMaskIntoConstraints = NO;
tableView.backgroundColor = [NSColor redColor];
tableView.headerView = nil;
tableView.delegate = self;
tableView.dataSource = self;
tableView.rowHeight = 21; // matches cell height
tableView.intercellSpacing = NSMakeSize(1,1);
NSNib *nib = [[NSNib alloc] initWithNibNamed:@"SHDataTableCellView" bundle:[NSBundle mainBundle]];
[tableView registerNib:nib forIdentifier:@"SHDataTableCellView"];
// Setup scrollView
scrollView.backgroundColor = [NSColor greenColor];
scrollView.hasVerticalScroller = YES;
scrollView.hasHorizontalScroller = YES;
scrollView.autohidesScrollers = YES;
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
scrollView.documentView = tableView;
scrollView.contentView.drawsBackground = NO;
scrollView.scrollerStyle = NSScrollerStyleOverlay;
scrollView.automaticallyAdjustsContentInsets = NO;
scrollView.contentInsets = NSEdgeInsetsMake(0,0);
self.scrollView = scrollView;
[view addSubview:scrollView];
// Add constraints
NSDictionary *viewBindings = NSDictionaryOfVariableBindings(view,scrollView);
[view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(0)-[scrollView]-(0)-|" options:0 metrics:nil views:viewBindings]];
[view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(0)-[scrollView]-(0)-|" options:0 metrics:nil views:viewBindings]];
正如您在下面看到的,一切正常。我想知道如何删除单元格周围的外部“较厚”边框,如下所述。我没有找到任何插入设置或类似设置。
这就是我想要的:
如果您需要更多代码,请告诉我(尽管我认为这是相关部分)
更新:
这是我用来创建单元格的与视图相关的委托方法
// ################################################
- (NSView*)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
// Cell view is from a nib file which just has a text field with leading/trailing constaint = 0,height constraint = 21
// Nib registered in table setup
SHDataTableCellView *result = [tableView makeViewWithIdentifier:@"SHDataTableCellView" owner:tableView];
// Height constraint,width is dynamic
[result addConstraint:[NSLayoutConstraint constraintWithItem:result
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:21]];
result.dataValue.stringValue = [[[self.datasourceController arrangedObjects] objectAtIndex:row] objectAtIndex:[tableColumn.identifier integerValue]];
return result;
}
为了更完整,我在单元格视图中添加了一个蓝色边框
self.wantsLayer = YES;
self.layer.borderColor = [NSColor blueColor].CGColor;
self.layer.borderWidth = 2.0f;
看起来像这样:
所以我假设它不是单元格视图问题,而是位于表格视图本身的某个地方(表格具有红色背景色)。
解决方法
我在 macOS 10.15 上测试应用程序时偶然发现了解决方案。
它在 Catalina 中看起来不错,但在 BigSur 中却不是,所以我仔细看了看,我找到了 BigSur 中首先可用的表格样式的解决方案。在表设置中添加此代码后,它在两个系统中看起来都不错。
if (@available(macOS 11.0,*)) {
tableView.style = NSTableViewStylePlain;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。