简体   繁体   English

斯威夫特 5 | didSelectRowAt 同时选择两个单元格

[英]Swift 5 | didSelectRowAt is selecting two cells at the same time

I am doing a screen where there a list o cells with a switch, like an image below;我正在做一个屏幕,其中有一个带有开关的单元格列表,如下图所示; I have a struct where a save the label of the cell and the switch state value.我有一个结构体,其中保存了单元格的标签和开关状态值。 This struct is loaded at: var source: [StructName] = [] and then source values are attributed to the UITableView cells.这个结构被加载在: var source: [StructName] = []然后源值被归于 UITableView 单元格。

The problem is that when a touch a cell the function: func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) change multiples cells switches states at the same time.问题是当触摸一个单元格时,函数: func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)改变多个单元格同时切换状态。 I try to work around the problem by implementing the following function:我尝试通过实现以下功能来解决这个问题:

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)

    let cell = tableView.cellForRow(at: indexPath) as! CustomTableViewCell
    for n in 0..<source.count{ // This loop search for the right cell by looking at the cell label text and the struct where the state of the switch is saved
        if cell.label.text! == source[n].Label{
            // If the label text is equal to the position where the values is saved (is the same order that the cells are loaded in the UITableView) then a change the state of the switch
            let indexLabel = IndexPath(row: n, section: 0)
            let cellValues = tableView.cellForRow(at: indexLabel) as! CustomTableViewCell
            if cellValues.switchButton.isOn {
                cellValues.switchButton.setOn(false, animated: true)
                source[n].valor = cellValues.switchButton.isOn
            } else {
                cellValues.switchButton.setOn(true, animated: true)
                source[n].valor = cellValues.switchButton.isOn
            }
            break
        }
    }

although is saved the right values to the switch state array(source) the visual state of multiples switches also changes even though the cells where never touch.尽管将正确的值保存到开关状态数组(源)中,但多个开关的视觉状态也会发生变化,即使从未接触过的单元格也是如此。

How could I change my code to select and change only the touched cell?如何更改我的代码以选择和更改触摸的单元格?

在此处输入图片说明

You should not store / read the state of anything in a cell.您不应在单元格中存储/读取任何内容的状态。 But first things first:但首先要注意的是:

  • Why do loop through all the values?为什么要遍历所有值? You should be able to access the row in the data model directly by indexPath.row您应该能够通过indexPath.row直接访问数据模型中的行
  • You should only modify the model data, not the cell您应该只修改模型数据,而不是单元格
  • You then tell the table view to reload the cell, which will then ask the model for the correct data to be displayed.然后你告诉表格视图重新加载单元格,然后它会要求模型显示正确的数据。

I would suggest the following:我建议如下:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)

    let row = indexPath.row
    source[row].valor.toggle()
    tableView.reloadRows(at:[indexPath], with:.automatic)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM