cell reuse & disposebag
For my project I've made base cell
class TableViewCell: UITableViewCell {
private(set) var disposeBag = DisposeBag()
override func prepareForReuse() {
super.prepareForReuse()
disposeBag = DisposeBag() // because life cicle of every cell ends on prepare for reuse
}
}
and now in your cell factory you should do:
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! DiaryItemCell
cell.commentButton.rx_tap
.subscribeNext{
showAlert("Hi")
}.addDisposableTo(cell.disposeBag)
return cell
浙公网安备 33010602011771号
All disposables (CompositeDisposable, SerialDisposable, AnonymousDisposable ...) have the same behavior.
addDisposablewill calldisposeimmediately (@sergdot that's whyself.compositeDisposable.dispose()was causing that weird behavior ;)All classes named
*Disposableare meant to be used while implementing your own Rx operators.DisposeBagis meant to return ARC like memory management to subscriptions, and it will dispose all subscriptions (Disposables) it contains on deinit.Hope this clears things up :)