订阅位于UaithitViewDataSource内UaiteDViewCell中的Uibutton.rx.TAP



假设我在UITableViewCell中有一个UIButton。从UITableView脱水后,我想订阅UIButton.rx.tap。问题是,如果我的UITableViewCell被多次脱水,则订阅将保留。目前,我通过在我的UITableViewCell中分配Disposable属性来解决此问题,在创建订阅时将其设置,并在UITableViewCell.prepareForReuse()上调用Disposable.dispose(),但是,据我了解,我了解以一种需要您调用Disposable.dispose()的方式实现功能,则意味着您在做。有问题。

有没有更好的方法来完成订阅的唯一性,而无需重新关注UIButton

另一种解决方案(不需要其他库或调用Disposable.dispose())是在单元格中具有DisposeBag并将其重新创建在prepareForReuse中,如本github问题所示:

//in the cell 
private(set) var disposeBag = DisposeBag()
override func prepareForReuse() {
   super.prepareForReuse()
   disposeBag = DisposeBag()
}

//in the data source
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! DiaryItemCell
cell.commentButton.rx_tap
            .subscribeNext{
            }.addDisposableTo(cell.disposeBag)
return cell

,如果您在单元格中有更多按钮(或其他要订阅的观察值),它也将起作用。您无需为每个单元格创建新的Disposable

您可以使用UITableViewCell中的反应性订阅使用Cell-Rx Pod形式正确。对于您的情况,您可以使用rx_reusableDisposeBag,它可以正确处理您的订阅。