I have code that creates an NSTableView in a NSScrollView. It works but I end up with borders on the left and right side of the displayed table. The table call in ContextView has a frame but changing it does not remove the borders. I have set column widths in the makeNSView function which lets me control the width of the columns but the borders remain. Assistance would be appreciated. Below is my table code along with what gets displayed.
// in context view:
BetterTableView()
.frame(width: 350, height: 150, alignment: .center)
// better table view and associated data
struct ClosingValue: Identifiable {
var id: UUID
var name: String
var date: String
var close: Float
static var closingValues = [
ClosingValue(id: UUID(), name: "Stock1", date: "Nov 01, 2009", close: 1.10),
ClosingValue(id: UUID(), name: "Stock1", date: "Nov 02, 2009", close: 2.10),
ClosingValue(id: UUID(), name: "Stock1", date: "Nov 03, 2009", close: 3.10),
ClosingValue(id: UUID(), name: "Stock1", date: "Nov 04, 2009", close: 4.10),
ClosingValue(id: UUID(), name: "Stock1", date: "Nov 05, 2009", close: 5.10),
ClosingValue(id: UUID(), name: "Stock1", date: "Nov 06, 2009", close: 6.10)
]
}
struct BetterTableView: NSViewRepresentable {
func makeCoordinator() -> Coordinator {
Coordinator()
}
class Coordinator: NSObject, NSTableViewDelegate, NSTableViewDataSource {
@State var closingValues: [ClosingValue] = ClosingValue.closingValues
func numberOfRows(in tableView: NSTableView) -> Int {
closingValues.count
}
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
var nsView = NSView()
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let attributes1: [NSAttributedString.Key: Any] = [
.foregroundColor: NSColor.blue,
.paragraphStyle: paragraphStyle,
.font: NSFont(name: "Arial", size: 14.0) as Any
]
var attributedString = NSAttributedString()
let tempNSView = NSTextField()
switch tableColumn {
case tableView.tableColumns[0]:
attributedString = NSAttributedString(string: closingValues[row].name, attributes: attributes1)
case tableView.tableColumns[1]:
attributedString = NSAttributedString(string: closingValues[row].date, attributes: attributes1)
case tableView.tableColumns[2]:
let closeAsString = String(format: "$%.2f", closingValues[row].close)
attributedString = NSAttributedString(string: closeAsString, attributes: attributes1)
default:
print("problem in table view switch statement")
}
tempNSView.backgroundColor = NSColor.white
tempNSView.isBordered = true
tempNSView.isEditable = false
tempNSView.attributedStringValue = attributedString
nsView = tempNSView
return nsView
}
} // end of coordinator class
func makeNSView(context: Context) -> NSScrollView {
let tableView = NSTableView()
tableView.delegate = context.coordinator
tableView.dataSource = context.coordinator
tableView.addTableColumn(NSTableColumn())
tableView.addTableColumn(NSTableColumn())
tableView.addTableColumn(NSTableColumn())
tableView.intercellSpacing = NSSize(width: 0.0, height: 0.0)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let attributes: [NSAttributedString.Key: Any] = [
.foregroundColor: NSColor.black,
.paragraphStyle: paragraphStyle,
.font: NSFont.systemFont(ofSize: 16)
]
let column0AttributedString = NSAttributedString(string: "Stock", attributes: attributes)
let column0Header = tableView.tableColumns[0]
column0Header.headerCell.drawsBackground = true
column0Header.headerCell.backgroundColor = NSColor.systemMint
column0Header.headerCell.alignment = .center
column0Header.headerCell.attributedStringValue = column0AttributedString
column0Header.sizeToFit()
column0Header.width = 90.0
let column1AttributedString = NSAttributedString(string: "Date", attributes: attributes)
let column1Header = tableView.tableColumns[1]
column1Header.headerCell.drawsBackground = true
column1Header.headerCell.backgroundColor = NSColor.systemMint
column1Header.headerCell.alignment = .center
column1Header.headerCell.attributedStringValue = column1AttributedString
column1Header.sizeToFit()
column1Header.width = 130.0
let column2AttributedString = NSAttributedString(string: "Closing", attributes: attributes)
let column2Header = tableView.tableColumns[2]
column2Header.headerCell.drawsBackground = true
column2Header.headerCell.backgroundColor = NSColor.systemMint
column2Header.headerCell.alignment = .center
column2Header.headerCell.attributedStringValue = column2AttributedString
column2Header.sizeToFit()
column2Header.width = 70.0
let scrollView = NSScrollView()
scrollView.documentView = tableView
return scrollView
}
func updateNSView(_ nsView: NSScrollView, context: Context) {
let tableView = (nsView.documentView as! NSTableView)
// work on this section
}
}
Set tableView.style = .plain
. Here's the documentation.