【问题标题】:NSTableview with RXSwift and RxCocoa for OSXNSTableview 与 RXSwift 和 RxCocoa for OSX
【发布时间】:2018-06-12 19:40:09
【问题描述】:

如何使用响应式框架用数组填充 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

实现同样的效果

【问题讨论】:

    标签: macos reactive-programming nstableview rx-swift rx-cocoa


    【解决方案1】:

    我遇到了类似的需求,并通过 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
    }
    
    
    

    【讨论】:

      【解决方案2】:

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

      阅读此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)
      }
      

      【讨论】:

      • 请参考我修改后的帖子。我指的是 appkit 的 NSTableview。
      • @Padma 很抱歉,我无法为您提供帮助
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-26
      • 2018-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多