I am trying to build a table view without interface builder from scratch in my Cocoa app and my viewForTableColumn
method refuses to get called.
My table view is created using this code.
let tableView: NSTableView = {
let table = NSTableView(frame: .zero)
table.headerView = nil
table.addTableColumn(NSTableColumn(identifier: .column))
return table
}()
I also have this extension for the NSUserInterfaceItemIdentifier
class
extension NSUserInterfaceItemIdentifier {
static let cell = NSUserInterfaceItemIdentifier("cell")
static let column = NSUserInterfaceItemIdentifier("column")
}
My view controller is implementing NSTableViewDataSource
and NSTableViewDelegate
and they are set in the viewDidLoad
method. The table view is also into a scroll view.
let scrollView = NSScrollView()
override func viewDidLoad() {
self.scrollView.documentView = self.tableView
self.scrollView.hasVerticalScroller = true
self.view.addSubview(self.scrollView)
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.reloadData() // To make sure the method would be called.
}
The value returned from the numberOfRows
method is bigger than 0
and is executed.
From my understanding the following method should be called with the row parameter ranging from 0 to the value returned by numberOfRows
but it's never being called.
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
guard let view = self.tableView.makeView(withIdentifier: .cell, owner: self) as? NSTableCellView else {
return nil
}
// set the text/font for the view
return view
}
I've also tried using dataCellFor
and rowViewForRow
but none of them is getting called.
EDIT:
I've checked the frame of my elements to make sure the table view and the rows were visible.