• 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
     
  • 相关阅读:
    HDU 1982 Kaitou Kid The Phantom Thief (1)
    HDU 1984 Mispelling4
    HDU 2546 饭卡
    HDU 1009 FatMouse' Trade
    在VC 中如何隐藏一个主程序窗口
    .菜单项
    SetClassLong,GetClassLong 动态改变光标
    .窗口捕获鼠标
    .主窗口向子控件发送消息
    线段树 1698 Just a Hook 区间set更新
  • 原文地址:https://www.cnblogs.com/Jenaral/p/5638913.html
Copyright © 2020-2023  润新知