NSTableViewDiffableDataSource bug? table view delegate method not called

3 days ago 7
ARTICLE AD BOX

The problem is that when I use a diffable data source, the delegate method tableView(_:rowViewForRow:) is not being called.

It will be easiest to show the problem if I just display the complete code for a view controller with a table view property, where the table view is configured in the storyboard.

This example works fine, where "works" means that the delegate method tableView(_:rowViewForRow:) is called as expected:

class ViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate { @IBOutlet var tableView: NSTableView! { didSet { tableView.delegate = self tableView.dataSource = self } } func numberOfRows(in tableView: NSTableView) -> Int { return 3 } func tableView(_ tableView: NSTableView, viewFor column: NSTableColumn?, row: Int) -> NSView? { let view = tableView.makeView( withIdentifier: column?.identifier ?? .init("dummy"), owner: tableView ) as? NSTableCellView view?.textField?.stringValue = ["Manny", "Moe", "Jack"][row] return view ?? NSTableCellView() } func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? { print("here") // CALLED 3 TIMES return NSTableRowView() } }

Now I'll replace the old-fashioned data source code with a diffable data source; the delegate method is not called (though other delegate methods, not shown, are called as expected):

class ViewController: NSViewController, NSTableViewDelegate { @IBOutlet var tableView: NSTableView! { didSet { tableView.delegate = self datasource = NSTableViewDiffableDataSource<String, String>( tableView: tableView ) { (tableView, column, row, string) in let view = tableView.makeView( withIdentifier: column.identifier, owner: tableView ) as? NSTableCellView view?.textField?.stringValue = ["Manny", "Moe", "Jack"][row] return view ?? NSTableCellView() } var snapshot = datasource.snapshot() snapshot.appendSections(["dummy"]) snapshot.appendItems(["manny", "moe", "jack"]) datasource.apply(snapshot, animatingDifferences: false) } } var datasource: NSTableViewDiffableDataSource<String, String>! func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? { print("here") // NOT CALLED return NSTableRowView() } }

Is this a known issue? Is there a workaround? Or should I just throw up my hands, report a bug, and move on — meaning that, since I need this delegate method to be called, I'll just have to revert to the old-fashioned code...?

Read Entire Article