I have a program that creates a custom table view. In the delegate that populates the cells with data I create an instance of a custom NSTableCellView. It works. What I would like to be able to do is pass in a value when making an instance so that I can set the width of the frame (var rectWidth in the custom NSTableCellView). I am currently using two different custom NSTableCellViews, each with a different value for var rectWidth. Below is my delegate and custom NSTableCellView code.
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
if (tableColumn?.identifier)!.rawValue == "fund" {
let dataCell = CustomTableCellView()
dataCell.textField?.stringValue = closingValues[row].fundName
return dataCell
} else if (tableColumn?.identifier)!.rawValue == "date" {
let dataCell = CustomTableCellViewDate()
dataCell.textField?.stringValue = SQLDateFormatter.string(from: closingValues[row].timeStamp)
return dataCell
} else {
let dataCell = CustomTableCellView()
dataCell.textField?.stringValue = String(format: "$%.2f", closingValues[row].close)
return dataCell
}
}
class CustomTableCellView: NSTableCellView {
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
let rectWidth: CGFloat = 100
self.autoresizingMask = .width
let nsRectangle = NSMakeRect(0, 0, rectWidth, 24)
let customTextField: NSTextField = CustomTextField(frame: nsRectangle)
self.textField = customTextField
self.addSubview(customTextField)
}
required init?(coder decoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}