-4

My Switch animation stuck somewhere between on and off. It happens after the switch goes out of screen bounds (The switch is inside the table view cell). I don't face this issue if I don't let the cell to go out of the screen bounds. I've set breakpoints to figure out what's happening and realized that this happens at the moment I change the switch value. The GIFs below are for a better understanding of what I mean. The first one shows the behavior I'd like to fix. The second one is OK.

[Weird Switch Behavior1

Nornal Switch Behavior

Roma Kavinskyi
  • 268
  • 4
  • 12

1 Answers1

0

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)
    ])
}
Roma Kavinskyi
  • 268
  • 4
  • 12