4

Let's say I have a UIButton in a UITableViewCell. After dequeuing the cell from the UITableView I want to subscribe to the UIButton.rx.tap. The issue is that if my UITableViewCell is dequeued multiple times, the subscriptions would retain. Currently I solve this problem by allocating a Disposable property in my UITableViewCell, setting it when the subscription is create, and calling Disposable.dispose() on UITableViewCell.prepareForReuse(), however as far as I understand implementing features in a way that requires you to call Disposable.dispose() implies that you are doing something wrong.

Is there any better way to accomplish uniqueness of the subscription without reallocating UIButton?

Timofey Solonin
  • 1,393
  • 13
  • 20

2 Answers2

13

Another solution (which doesn't require an additional library or calling Disposable.dispose()) is to have a DisposeBag in the cell and re-create it in prepareForReuse, as suggested in this GitHub issue:

//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

It will also work if you have more buttons (or other Observables which you want to subscribe to) in your cell. You won't have to create a new Disposable in the cell itself for each of them.

Michał Ciuba
  • 7,876
  • 2
  • 33
  • 59
1

You can use Cell-Rx pod form correct using reactive subscriptions in UITableViewCell. For your case you can use rx_reusableDisposeBag, it will dispose your subscriptions correct.

Artem Novichkov
  • 2,356
  • 2
  • 24
  • 34
  • I forgot to call super.prepareForReuse() in my `UITableViewCell`. That's why the `rx_reusableDisposeBag` wan't resetting. Thanks for the suggestion! – Timofey Solonin Dec 11 '16 at 12:11