[Solved] Weird behavior of the UISwitch


I managed to fix the problem by myself. I didn’t post the code, because there was too much of it, and I simply could post it all here. But if someone has the same problem – here is the solution.

Turns out, that while cells are being reused ( cell goes off the screen), all the content isn’t deleted from them. After they appeared on the screen again I ended up having multiple switches stacked on each other. All I did to fix it was adding switch.removeFromSuperview() to my cell.set method before initializing a new switch.

 func setSwitch() {
    switchControl?.removeFromSuperview()
    lockedPic.removeFromSuperview()
    switchControl = CustomSwitch(number: self.tag)
    contentView.addSubview(switchControl!)
    
    NSLayoutConstraint.activate([
        switchControl!.leadingAnchor.constraint(equalTo: self.trailingAnchor, constant: -61),
        switchControl!.centerYAnchor.constraint(equalTo: self.centerYAnchor)
    ])
}

solved Weird behavior of the UISwitch