I have a swift program that displays a custom table. Originally I incorrectly used a NSTextField for each cell and ran into a formatting issue. As can be seen in the picture below, the text in each cell is up against the top border. I was unable to figure out how to solve the problem so I posted my original code below. A response suggested that I change the NSTextField to a NSTableCellView. So I changed my code (new code is now below) to incorporate the NSTableCellView. Unfortunately, I ended up with the same formatting problem. I thought I could apply a constraint to the NSTableCellView relative to its parent but with no success (attempt commented out in delegate). It would be appreciated if someone could point me in the right direction to solve this issue. If I need to post more of my code, please let me know.
// Coordinators delegate
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
let dataCell = CustomTableCellView()
switch tableColumn?.identifier.rawValue {
case "fund":
dataCell.Setup(rectWidth: 100.0, row: row, column: tableColumn!, numberOfRows: closingValues.count)
dataCell.textField?.stringValue = closingValues[row].fundName
case "date":
dataCell.Setup(rectWidth: 120.0, row: row, column: tableColumn!, numberOfRows: closingValues.count)
dataCell.textField?.stringValue = SQLDateFormatter.string(from: closingValues[row].timeStamp)
default:
dataCell.Setup(rectWidth: 100.0, row: row, column: tableColumn!, numberOfRows: closingValues.count)
dataCell.textField?.stringValue = String(format: "$%.2f", closingValues[row].close)
}
// let constraints = [
// dataCell.topAnchor.constraint(equalTo: ??parent??.topAnchor, constant: 5.0)
// ]
// NSLayoutConstraint.activate(constraints)
return dataCell
}
// Custom NSTableCellView
class CustomTableCellView: NSTableCellView {
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
}
func Setup(rectWidth: CGFloat, row: Int, column: NSTableColumn, numberOfRows: Int) {
self.autoresizingMask = .width
let nsRectangle: NSRect = NSMakeRect(0, 0, rectWidth, 24)
let customTextField: CustomTextField = CustomTextField(frame: nsRectangle)
customTextField.SetTextAttributes(row: row, column: column, numberOfRows: numberOfRows)
self.textField = customTextField
self.addSubview(customTextField)
}
required init?(coder decoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}