Ios 订阅UITableViewDataSource中UITableViewCell中的UIButton.rx.tap
假设我在Ios 订阅UITableViewDataSource中UITableViewCell中的UIButton.rx.tap,ios,uibutton,rx-swift,reactivex,rxdatasources,Ios,Uibutton,Rx Swift,Reactivex,Rxdatasources,假设我在UITableViewCell中有一个UIButton。 从UITableView中退出单元格队列后,我想订阅UIButton.rx.tap。问题是,如果myUITableViewCell多次退出队列,订阅将保留。目前,我通过在我的UITableViewCell中分配一个Disposable属性,在创建订阅时设置它,并在UITableViewCell.prepareforuse()上调用dispose.dispose(),来解决这个问题,然而,据我所知,以一种需要调用Disposable
UITableViewCell
中有一个UIButton
。
从UITableView
中退出单元格队列后,我想订阅UIButton.rx.tap
。问题是,如果myUITableViewCell
多次退出队列,订阅将保留。目前,我通过在我的UITableViewCell
中分配一个Disposable
属性,在创建订阅时设置它,并在UITableViewCell.prepareforuse()上调用dispose.dispose()
,来解决这个问题,然而,据我所知,以一种需要调用Disposable.dispose()
的方式实现特性意味着您做错了什么
有没有更好的方法来实现订阅的唯一性,而无需重新分配UIButton
?您可以在UITableViewCell
中使用响应式订阅来使用正确的pod表单。对于您的情况,您可以使用rx\u reusableDisposeBag
,它将正确地处理您的订阅。另一种解决方案(不需要额外的库或调用Disposable.dispose()
)是在单元格中设置一个DisposeBag
,并在prepareforeuse
中重新创建它,如下所示:
如果您的单元格中有更多的按钮(或您想要订阅的其他观察对象),它也会起作用。您不必在单元格中为每个单元格创建一个新的一次性
我忘记在我的UITableViewCell
中调用super.prepareforuse()。这就是为什么rx\u reusableDisposeBag
wan不能重置的原因。谢谢你的建议!我开始意识到你的解决方案其实要优雅得多。不过,最好是开发一个DisposeBase
,无需重新实例化即可进行处置。非常感谢:)使我免于额外的库和黑客攻击。
//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