Роман Елизаров Роман Елизаров - 23 days ago 12
Swift Question

NSTableView viewFor tableColumn delegate method not called

In code I have something like this:

let scrollView = NSScrollView()
let tableView = NSTableView()

override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.

tableView.delegate = self
tableView.dataSource = self
tableView.headerView = nil

scrollView.documentView = tableView
scrollView.hasVerticalScroller = true

self.view.addSubview(scrollView)

}


Both required methods (
numberOfRows
and from the title) are declared. TableView is visible in view, scrollable and selectable.
tableViewSelectionDidChange
works properly, I can get index of selected row by
tableView.selectedRow
. But all cells are empty, because
tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int)
called for none of the cells. I think, the problem is that tableView can't figure out which of the cells are visible right now. But I don't know what I should do with that

Answer

Well, I struggled for a moment, but it became clear quickly. The tableView generated by you is fine.

The only thing that is missing is a column. You can simply add one column:

tableView.addTableColumn(NSTableColumn(identifier: "Column"))

Table Views display their views inside columns. Most tableViews have only one column and with the header hidden, we forget that they even exist.

Without any column, everything else works still normally. BackgroundRows load and selection works - which made me get to this solution.