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









All disposables (CompositeDisposable, SerialDisposable, AnonymousDisposable ...) have the same behavior.

  • once they are disposed, adding another disposable with addDisposable will call disposeimmediately (@sergdot that's why self.compositeDisposable.dispose() was causing that weird behavior ;)
  • they don't call dispose automatically on deinit

All classes named *Disposable are meant to be used while implementing your own Rx operators.

DisposeBag is 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 :)

 
@kzaher

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值