【问题标题】:NSTableView remove padding/Space between Table view border and cellsNSTableView 删除表格视图边框和单元格之间的填充/空格
【发布时间】:2020-12-22 21:40:04
【问题描述】:

我使用的是 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,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;

看起来像这样:

所以我认为它不是单元格视图问题,而是位于表格视图本身的某个位置(表格具有红色背景色)。

【问题讨论】:

  • 是否实现了任何与视图相关的NSTableViewDelegate 方法?
  • 单元格是。该单元格具有白色背景,因此您可以在屏幕截图中直观地看到它们的框架。代码已更新。

标签: objective-c nstableview


【解决方案1】:

我在 macOS 10.15 上测试应用程序时偶然发现了该解决方案。

它在 Catalina 中看起来不错,但在 BigSur 中却不行,所以我仔细看了看,我发现了 BigSur 中首先可用的表格样式的解决方案。在表格设置中添加此代码后,它在两个系统中看起来都不错。

 if #available(macOS 11.0, *) {
     table.style = NSTableView.Style.plain
 }

【讨论】:

  • 我稍微更新了语法。和伟大的发现。谢谢。