1

首先对令人困惑的标题感到抱歉,但我实际上无法想出更好的东西(如果你这样做,请编辑)。

我有一个使用 Coordinator 模式和 RxSwift 的应用程序,所以总而言之,我想将所有与导航相关的东西传递给 Coordinator,这样它就可以处理导航逻辑。
在一个视图控制器中,我拥有UITableView其中包含单元格的单元格UIButton。对于这种情况,我有一个:

actionButton.rx.tap.bind(to: viewModel.chapterAction).disposed(by: disposeBag)

chapterAction是一个PublishSubject<Void>,因为它只反映了一个按钮点击,但我需要将更多信息传递给协调器,所以稍后我将其转换chapterAction为:

var showChapter: Observable<Chapter> = self.chapterAction.mapTo(self.chapter)

而且我认为到目前为止这段代码没有任何问题,所以在 View Controller 中.bind(to: tableView.rx.items...我有:

viewModel.showChapter.bind(to: self.viewModel.chapterAction).disposed(by: viewModel.disposeBag)

因为我想将它绑定到视图控制器viewModel,然后subscribe在协调器中。
一切正常,但是对于某些单元格,我会重复点击,为什么?我试过把distinctUntil, shareReply,但似乎没有任何帮助我的问题,它不是一个确定性的问题。我怀疑涉及到一些重用,但我不知道从哪里开始寻找这个问题......

4

2 回答 2

3

您需要重新初始化disposeBaginprepareForReuse()方法:

override func prepareForReuse() {
    super.prepareForReuse()
    disposeBag = DisposeBag()
}

然后,所有以前的订阅都被处理掉。

于 2017-10-24T15:38:41.300 回答
0

我仍然不知道我的代码到底出了什么问题,但我最终mapTo直接在tableView.rx.items绑定块中添加。现在看起来像这样:

viewModel.chapterCellViewModels
    .bind(to: tableView.rx.items(cellIdentifier: ChapterCell.nameOfClass, cellType: ChapterCell.self )) { (_, viewModel, cell) in
        cell.configureCell(with: viewModel)

        cell.actionButton.rx.tap
            .mapTo(viewModel.chapter)
            .bind(to: self.viewModel.chapterAction)
            .disposed(by: cell.disposeBag)
    }
    .disposed(by: disposeBag)

正如我在 OP 中提到的 - 我想这是由于一些重用等,但我无法回答为什么会发生这种情况。

于 2017-11-06T08:49:24.590 回答