Swift 3 UISwitch in TableViewCell loses State when scrolling

后端 未结 3 1557
感情败类
感情败类 2020-12-12 07:38

That\'s weird: I just set up a new Single-View iOS Project and put a TableView into the Main.storyboard. In this TableView I put a TableViewCell and into this Cell, I put an

3条回答
  •  不知归路
    2020-12-12 08:18

    Add function prepareForReuse() in your custom cell class and set switch state to off by default. -:

     class MyTableViewCell: UITableViewCell {
        @IBOutlet weak var myLabel: UILabel!
        @IBOutlet weak var mySwitch: UISwitch!
    
        override func awakeFromNib() {
            super.awakeFromNib()
            // Initialization code
        }
    
        override func setSelected(_ selected: Bool, animated: Bool) {
            super.setSelected(selected, animated: animated)
    
            // Configure the view for the selected state
        }
    
        @IBAction func switched(_ sender: UISwitch) {
            print("Switched: \(sender.isOn)")
        }
    
        override func prepareForReuse(){
        // SET SWITCH STATE OFF HERE
        }
    

提交回复
热议问题