Tim*_*nin 2 uibutton ios rx-swift reactivex rxdatasources
假设我有UIButton一个UITableViewCell.从UITableView我想要订阅的单元格出列后UIButton.rx.tap.问题是如果我UITableViewCell多次出列,订阅将保留.目前我通过Disposable在my中分配属性,在UITableViewCell创建订阅时设置它并调用Disposable.dispose()来解决这个问题UITableViewCell.prepareForReuse(),但据我所知,实现功能的方式要求你调用Disposable.dispose()暗示你做错了.
有没有更好的方法来实现订阅的唯一性而无需重新分配UIButton?
Mic*_*uba 11
另一个解决方案(不需要额外的库或调用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
Run Code Online (Sandbox Code Playgroud)
如果您的单元格中有更多按钮(或您想要订阅的其他Observable),它也会起作用.您不必Disposable为每个单元格创建一个新的单元格.
| 归档时间: |
|
| 查看次数: |
1983 次 |
| 最近记录: |