I have a simple cell which contains text and switchView
final class MFTestLabel: SUCollectionCell {
// MARK: - Properties
weak var delegate: MFTestLabelDelegate?
private let containerView = UIView()
private let label = UILabel()
private let switchView = SUSwitch()
override func drawSelf() {
label.numberOfLines = 0
label.textAlignment = .center
label.isUserInteractionEnabled = true
containerView.layer.cornerRadius = 16
containerView.backgroundColor = .lightGray
switchView.addTarget(self, action: #selector(didTapSwitch), for: .touchUpInside)
contentView.addSubview(containerView)
containerView.addSubview(switchView)
containerView.addSubview(label)
}
@objc private func didTapSwitch() {
var state = configurationState
state.isSelected = true
updateConfiguration(using: state)
}
override func makeConstraints() {
containerView.snp.makeConstraints { make in
make.top.bottom.equalToSuperview().inset(4)
make.height.equalTo(40)
make.leading.trailing.equalToSuperview().inset(4)
}
switchView.snp.makeConstraints { make in
make.centerY.equalToSuperview()
make.trailing.equalToSuperview().inset(10)
}
label.snp.makeConstraints { make in
make.center.equalToSuperview()
}
}
And here is a small code where I have a question
@objc private func didTapSwitch() {
var state = configurationState
state.isSelected = true
updateConfiguration(using: state)
}
Why apple dont recommend using updateConfiguration(using: state)
? According to this documentation https://developer.apple.com/documentation/uikit/uicollectionviewcell/3600950-updateconfiguration
Quote
Avoid calling this method directly. Instead, use setNeedsUpdateConfiguration() to request an update.
Override this method in a subclass to update the cell’s configuration using the provided state.
I have tried to follow code samples from apple repositories