Search code examples
iosswiftuicollectionviewuicollectionviewcell

Manually call updateConfiguration swift


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 does not 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.


Solution

  • Some methods are provided for you to call, and some are provided for you to override so that the runtime can call them.

    Thus, you never call

    • layoutSubviews
    • viewDidAppear(_:)
    • viewDidLoad
    • draw()

    and many others; you override (implement) them if needed. For some of these, there are methods you can call to tell the runtime to call them. Thus, for instance, to make layoutSubviews be called, you call setNeedsLayout() or layoutIfNeeded(). To make draw be called, you call setNeedsDisplay.

    Well, updateConfiguration(using:) is like that; it is there for you to override. To make updateConfiguration(using:) be called, you call setNeedsUpdateConfiguration.

    Just obey the rules. It's a framework: you need to cooperate with it or you'll just break it.