ios - CellForItemAt : index path not updating variable

标签 ios swift uitableview

我正在使用一个 UITableView,它有一个带有 UISwitch 的单元格。我有四个 tableViewCells,每个都来自同一个原型(prototype)单元格。但是,当我切换开关时,TableView CellForItemAt: 部分中的变量的唯一方法是当我拉动 tableView 使其离开屏幕并重新加载可重用单元格时。如何在切换开关时刷新这些变量?

这是我的代码:

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


    if indexPath.section == 0 {
        cell.textLabel?.text = OLLItems![indexPath.row]._text
        if indexPath.row == 0 {
            GlobalData.AllGlobalData.OLLImageState = cell.state //GlobalData.AllGlobalData.OLLImageState is an struct in another file
            print("OLLImageState \(GlobalData.AllGlobalData.OLLImageState)")




        }
        if indexPath.row == 1 {
            GlobalData.AllGlobalData.OLLAlgState = cell.state
            print("OLLAlgState \(GlobalData.AllGlobalData.OLLAlgState)")

        }

    }
    if indexPath.section == 1 {
        cell.textLabel?.text = PLLItems![indexPath.row]._text
        if indexPath.row == 0 {
            GlobalData.AllGlobalData.PLLImageState = cell.state
            print("PLLImageState \(GlobalData.AllGlobalData.PLLImageState)")



        }
        if indexPath.row == 1 {
            GlobalData.AllGlobalData.PLLAlgState = cell.state 
            print("PLLAlgState \(GlobalData.AllGlobalData.PLLAlgState)")

        }

    }
    return cell
}

最佳答案

尝试这样做

Below code works properly in my repo but I changed it a bit to justify your scenario

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

        cell.mySwitch.addTarget(self, action: #selector(switchChanged(_:)), for: .valueChanged)
        return cell
    }

    func switchChanged(_ mySwitch: UISwitch) {

      guard let cell = mySwitch.superview?.superview as? SwitchContainingTableViewCell else {
          return // or fatalError() or whatever
      }

      self.value = mySwitch.isOn   //define var value in your controller or do it locally 

      let indexPath = itemTable.indexPath(for: cell)        

      if indexPath.section == 0 {
          if indexPath.row == 0 {
               GlobalData.AllGlobalData.OLLImageState = self.value
          }
      }
    }

我添加了一个 switch 目标,在目标中我得到了开关的改变状态。通过使用 state = mySwitch.isOn

,可以在 cellForItemAt: 中完成相同的操作

希望这对您有所帮助!

关于ios - CellForItemAt : index path not updating variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55857285/

相关文章:

ios - 在安全加载 API 数据的同时显示 UIActivityIndi​​cator

ios - 循环遍历 URLS 的 NSArray - 崩溃 - iOS

arrays - Swift:将两个数组匹配成一个数组

javascript - Easeljs 可滚动容器

ios - 内部带有 UIImageView 的子类 UIView 不响应自动布局

swift - 如何在 appDelegate 方法上设置计时器计划?

swift - 表格 View 偏移

swift - 无法从 didReceiveLocalNotification 重新加载 UITable

用于解压缩文件的 iOS native 类或库?

ios - Xcode iOS 图像缓存