【发布时间】: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方法? -
单元格是。该单元格具有白色背景,因此您可以在屏幕截图中直观地看到它们的框架。代码已更新。