如何使用 RxDataSources 避免重复点击 Tableview 上的按钮

How to avoid duplicate tap on button on Tableview with RxDataSources

我有一个带有 RxDataSources 的 table 视图,其中的单元格项目有一个删除图标。当单元格出队并单击该删除图标时,将触发所有先前的单击事件,从而重复点击。 项目单元格:

 removeImageView.rx.tap().map { _ in indexPath } 
            .bind(to: viewModel.onRemoveItem).disposed(by: cellDisposeBag)

单元格视图模型:

let onRemoveItem = PublishSubject<IndexPath>()

单元格和 ViewModel 绑定的视图控制器视图模型:

 let vm = ItemViewModel(with: item)
            vm.onRemoveItem.bind(to: self.onRemoveItem).disposed(by: self.rx.disposeBag)

            return SectionItem.item(viewModel: vm)

视图控制器:

let dataSource = RxTableViewSectionedReloadDataSource<SectionItem>(configureCell: { dataSource, tableView, indexPath, item in
    switch item {
    case .item(let viewModel):
        let cell = (tableView.dequeueReusableCell(withIdentifier: itemtIdentifier, for: indexPath) as? ItemCell)!
        cell.bind(to: viewModel, at: indexPath)
        return cell
    }
}, titleForHeaderInSection: { dataSource, index in
    let section = dataSource[index]
    return section.title
}  )

output?.items
    .bind(to: tableView.rx.items(dataSource: dataSource))
    .disposed(by: rx.disposeBag)

output?.onRemoveCartIemTapped.distinctUntilChanged() 
    .skip(1)
    .distinctUntilChanged().drive(onNext: { [weak self] (indexPath) in
    print("onRemoveCartIemTapped" + String(indexPath.item))
}).disposed(by: rx.disposeBag)

控制台调试:

onRemoveCartIemTapped0
onRemoveCartIemTapped3
onRemoveCartIemTapped1
onRemoveCartIemTapped4

这是由于 UITableView 重复使用单元格造成的。为避免有多个订阅,您可以覆盖单元格的 prepareForReuse() 方法并确保处理所有现有订阅。

我通常将 DisposeBag 声明为 var,然后在 prepareForReuse() 中为其分配一个新的 DisposeBag。当 DisposeBag 被取消时,它将处理它包含的所有订阅。类似于:

override func prepareForReuse() {
    super.prepareForReuse()

    cellDisposeBag = DisposeBag()
}