6

如何使用响应式框架使用数组填充NSTableview ?在 iOS 中用于 UITableview:

self.viewModel.arrayElements.asObservable()
        .observeOn(MainScheduler.instance)
        .bind(to: detailsTableView.rx.items(cellIdentifier: "comment", cellType: UITableViewCell.self)){
            (row,element,cell) in
                 cell.addSubview(cellView)
        }.addDisposableTo(disposeBag)

我怎样才能为NSTableView实现相同的目标

在此处输入图像描述

4

2 回答 2

0

我遇到了类似的需求,并用BehaviorRelay(使用 RxSwift 5)解决了它。

BehaviorRelay 充当中介,因此可以使用常规NSTableViewDataSourceNSTableViewDelegate协议

重要的部分是self.detailsTableView.reloadData()告诉 tableview 重新加载数据的语句,它不会自动触发。

像这样的东西:

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
}


于 2020-06-01T07:58:38.970 回答
-1

试试下面的,你应该使用驱动程序而不是可观察的

阅读此https://github.com/ReactiveX/RxSwift/blob/master/Documentation/Traits.md

import RxSwift
import RxCocoa

let data = Variable<[String]>([])
let bag  = DisposeBag()

override func viewDidLoad() {
super.viewDidLoad()

    data.asDriver.drive( tableView.rx.items(cellIdentifier: "idenifier")){(row:Int, comment:String, cell:UITableViewCell) in
        cell.title = report
    }.disposed(by:bag)
}
于 2018-01-19T17:11:14.300 回答