【问题标题】:UISwitch is changing state on other cell when scroll滚动时 UISwitch 正在更改其他单元格上的状态
【发布时间】:2017-08-24 08:44:11
【问题描述】:

我的表格视图是这样的

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! Cell
    return cell
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 200
}

我的 tableviewcell 看起来像这样

class Cell: UITableViewCell {

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
}

}

当我更改任何单元格上的开关状态并滚动时。另一个单元格的开关会在滚动时定期更改其他单元格的状态。请帮忙

【问题讨论】:

  • 这是因为单元格出列。你能用数据模型更新你的问题吗?
  • 这就是问题所在。我创建了测试项目以查看我的数据模型是否会导致此问题。但是只有这些代码它给了我同样的问题
  • 请查看我更新的answer
  • 在数据模型中添加一个布尔标志(用于加载单元格数据)并在更新时保存开关状态。在您的 cellForRowAt 方法中,根据布尔标志相应地设置开关状态。

标签: ios swift uitableview uiswitch


【解决方案1】:

您需要为每个开关提供具有状态的数据数组,并在您的 didSelect 方法中更改开关的数据模型,然后在 cellForRow 中从数组中获取开关状态,并且您的 viewController 需要为 SwitchControlDelegate

class UIViewController:SwitchControlDelegate{

    var array = [Bool](count: 200, repeatedValue:false)

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! Cell
    cell.switchControl.isOn = array[indexPath.row]
    cell.switchDelegate = self
    cell.index = indexPath.row
    return cell
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 200
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    array[indexPath.row] = !array[indexPath.row]
}

func switchChangeStateWithIndex(index:Int){
    array[index] = !array[index]
}

这将在您单击单元格时更改开关状态,如果您需要在更改开关控制时更改状态,则需要委托来更新 viewController 中的数据模型

在您的单元格文件中:

    protocol SwitchControlDelegate: class {
        func switchChangeStateWithIndex(index:Int)
    }

    class Cell: UITableViewCell {

        var index:Int = 0
        weak var switchDelegate: SwitchControlDelegate?
        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 valueChanged(_ sender: AnyObject) {
             switchDelegate?.switchChangeStateWithIndex(index:index)
        }

}

您需要将开关控件的动作值更改与此函数 valueChanged 匹配,因此每次更改开关状态单元格时都需要调用 func value Changeed

【讨论】:

  • 嗨 Svetoslav,我的代码也有同样的问题。当我滚动时,其开关的选定行会发生变化。回到未选中状态。
  • 请查看我更新的代码,如果您仍然有问题,请将您的 viewController 和 cellFile 与所有 IBOutlets 和 IBActions 粘贴给我
  • 感谢新代码,如果它不起作用,将发布。但是你能告诉cell.delegate吗?它显示单元格没有委托方法的错误。请帮助解决此问题以测试您的代码
  • 我设法解决了这个问题。附加屏幕显示与我的问题的联系
  • 你需要匹配动作
【解决方案2】:

正确处理每个单元格的开关控制,

tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)

这样当单元格出列时,它会正确处理每个单元格的switchControl

解决方案: 当您更改单元格的开关控制状态时,将 indexPath 保存在某处,以便当您在 tableview cellForRowAt indexPath 中遍历此 indexPath 时,您再次设置该开关状态,否则设置默认状态

【讨论】:

    猜你喜欢
    • 2011-04-08
    • 1970-01-01
    • 1970-01-01
    • 2015-07-08
    • 1970-01-01
    • 2020-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多