首页 > 解决方案 > UITableViewCell 中的 UISwitches 一起改变状态

问题描述

我的 Swift 项目中有一个非常奇怪的问题。我在我的一节中的动态单元格中使用 UIswitch。每当我点击第 6 个开关时,第一个开关就会随之改变其状态,反之亦然。该.valueChanged功能仅适用于被点击的那个(正确的行为)。我无法弄清楚为什么开关会一起改变状态。

这是表格单元格的代码:

import UIKit

class RoutineTableViewCell: UITableViewCell {

    @IBOutlet weak var selectionSwitch: UISwitch!
    @IBOutlet weak var title: UILabel!
    @IBOutlet weak var previewImage: UIImageView!
    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

}

这是我的表格视图控制器中的代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: RoutineTableReuseIdentifier, for: indexPath) as? RoutineTableViewCell
            else{
                return RoutineTableViewCell()
        }

        let exercise = section!.exercises[indexPath.row]
        cell.title.text = exercise.title
        cell.previewImage.image = UIImage(named: (exercise.gifName + ".gif"))
        cell.selectionSwitch.addTarget(self, action: #selector(self.addRemoveExercise(_:)), for: .valueChanged)

        return cell
    }

    @IBAction func addRemoveExercise(_ sender: UISwitch!) {

        let buttonPosition:CGPoint = sender.convert(CGPoint.zero, to:self.tableView)
        let indexPath = self.tableView.indexPathForRow(at: buttonPosition)

        if(sender.isOn){
            customizedSection?.exercises[indexPath!.row] = section!.exercises[indexPath!.row]
        }
        else{
            customizedSection?.exercises[indexPath!.row] = ExerciseModel()
        }
    }

标签: swiftuitableview

解决方案


你的表视图:

import UIKit

class TableViewController: UITableViewController,switchValues {
    func changed(_ cell: UITableViewCell, _ mySwitch: UISwitch) {
        let index = tableView.indexPath(for: cell)
        switchStates[(index?.row)!] = mySwitch.isOn
        tableView.reloadData()
    }

    var switchStates : [Bool] = [false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false]
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return switchStates.count
    }


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyTableViewCell
    cell.mySwitch.isOn = switchStates[indexPath.row]
    cell.delegate = self
    return cell
}

你的手机:

import UIKit
protocol switchValues {
    func changed(_ cell:UITableViewCell,_ mySwitch:UISwitch)
}
class MyTableViewCell: UITableViewCell {

    @IBOutlet weak var mySwitch: UISwitch!
    var delegate:switchValues!
    @IBAction func valueChanged(_ sender: UISwitch) {
        delegate.changed(self, mySwitch)
    }
}

推荐阅读