I ran into a similar need, and solved it with a BehaviorRelay
(using RxSwift 5).
The BehaviorRelay acts as a mediator so it's possible to use regular NSTableViewDataSource
and NSTableViewDelegate
protocols
The important part is the self.detailsTableView.reloadData()
statement which tells the tableview to reload the data, it is not triggered automatically.
Something like this:
var disposeBag = DisposeBag()
var tableDataRelay = BehaviorRelay(value: [Element]())
func viewDidLoad() {
viewModel.arrayElements.asObservable()
.observeOn(MainScheduler.instance)
.bind(to: tableDataRelay).disposed(by: disposeBag)
tableDataRelay
.observeOn(MainScheduler.instance)
.subscribe({ [weak self] evt in
self.detailsTableView.reloadData()
}).disposed(by: disposeBag)
}
func numberOfRows(in tableView: NSTableView) -> Int {
return tableDataRelay.value.count
}
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
let element = tableDataRelay.value[row]
let cellView = tableView.makeView(withIdentifier: tableColumn!.identifier, owner: nil) as? NSTableCellView
cellView?.textField?.stringValue = element.comment
return cellView
}