码迷,mamicode.com
首页 > 其他好文 > 详细

cell reuse & disposebag

时间:2016-07-03 23:13:23      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

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

 
技术分享

 

 

cell reuse & disposebag

标签:

原文地址:http://www.cnblogs.com/Jenaral/p/5638913.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!