Cells are reusable views. When they leave the visible screen they may be reused by the parent view for another sibling element. For this reason, we need to have some considerations in mind.
setup
method, as always.
configure
method where we pass a model to finish the view.
DisposeBag
, re-assign it, so we remove previous callbacks or effects.final class SomeCell: UITableViewCell {
private var disposer = DisposeBag()
private let someView = UIView().apply { ... }
// MARK: Setup
private func setup() {
addSubview(someView)
someView.snp.makeConstraints { ... }
}
// MARK: Configure
public func configure(with model: Int) {
disposer = DisposeBag()
someView.backgroundColor = .clear
switch model {
case 0:
someView.backgroundColor = .green
case 1:
someView.backgroundColor = .blue
default:
break
}
}
}