UITableView 自定义单元格选项无法修改源变量

UITableView custom cell options can't modify the source variable

我刚开始学习 Xcode 和 swift,这是我在堆栈溢出中的第一个问题。所以请原谅我,如果我问的是一些琐碎的问题,或者我的问题定义不当。

所以我的问题如下: 我正在尝试创建一个设置视图,其中 TableView 使用自定义单元格。 我有两种类型的单元格:ValueCell(带有两个标签)和 SwitchCell(一个标签和一个开关)。我有两个单元格的 classes。我的 UITableViewController 包含用于填充表格视图的输入变量。经过一番努力,我可以做到这一点。所以我的 TableView 填充了我的所有设置。 所以问题如下: 当我与它交互时,如何让 Switch 修改原始值?

提前感谢您的帮助。

我的 SwitchCell class:

import Foundation
import UIKit

class SwitchCell: UITableViewCell {
    //SettingCell with switch
  @IBOutlet weak var name: UILabel!
  @IBOutlet weak var value: UISwitch!

  // I TRIED TO MAKE IT WORK, THIS IS CONNECTED TO THE SWITCH IN STORYBOARD
  @IBAction func switchStateChanged(_ sender: UISwitch) {
    if self.value.isOn {
        print("\((self.name.text)!)")
        print(self.value.isOn)
    } else {
        print("\((self.name.text)!)")
        print(self.value.isOn)
      }
  }

}

这是 BoolSetting class:

class BoolSetting: Setting {
 var value: Bool
 init (name: String, value: Bool) {
    self.value = value
    super.init(name: name, type: .Bool)
 }
}

我的设置列表项class:

class SettingsListItems: ListItems {
override init(){
    super.init()



    //MARK: Settings data structure

    addSection(section: "Image options", item: [
        IntSetting(name: "Resolution (dpi)", value: 1024),
        IntSetting(name: "Resolution quality", value: 8),
        IntSetting(name: "Zoom", value: 100)
        ])

    addSection(section: "Capture options", item: [
        BoolSetting(name: "Use flash", value:true),
        IntSetting(name: "Number of images to take", value: 2),
        FloatSetting(name: "Zero width (inch)", value: 0.405000),
        IntSetting(name: "Zero width (pixel)", value: 414),
        IntSetting(name: "Zero offset x (pixel)", value: 0),
        IntSetting(name: "Zero offset y (pixel)", value: -500)
        ])
    addSection(section: "Debug", item: [
        BoolSetting(name: "Debug mode", value: false),
        BoolSetting(name: "Save captured image", value: false)
        ])
}
}

最后是 SettingsViewController class:

 import UIKit

class SettingsViewController: UITableViewController {
let mySettings = SettingsListItems()



override func viewDidLoad() {
    super.viewDidLoad()


    // Do any additional setup after loading the view.

}

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(true)

    //Csak ellenőrzés, hogy változnak-e a setting értékek
    for item in mySettings.items {
        for s in item{
            switch s.type{
            case .Bool:
                print((s as! BoolSetting).value)
            case .Int:
                print((s as! IntSetting).value)
            case .Float:
                print((s as! FloatSetting).value)
            }

        }
    }
}



//hány section legyen a TableViewban
override func numberOfSections(in tableView: UITableView) -> Int {
    return mySettings.sections.count
}

//Hány row legyen egy section-ön belül
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return mySettings.items[section].count
}

//Mi legyen a cellák tartalma
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell: UITableViewCell

    //egyszerűsítő declarations
    let row = indexPath.row
    let section = indexPath.section
    let mySetting = mySettings.items[section] [row]

    //Setting type választó
    switch mySetting.type{
    case .Bool:
        cell = tableView.dequeueReusableCell(withIdentifier: "switchCell", for: indexPath)
        (cell as! SwitchCell).name.text = (mySetting as! BoolSetting).name
        (cell as! SwitchCell).value.isOn = (mySetting as! BoolSetting).value

 //This was what I tried to do, but I think something is very wrong here
        (cell as! SwitchCell).switchStateChanged(cell as! SwitchCell.value) {
            (mySetting as! BoolSetting).value = (cell as! SwitchCell).value.isOn
        }


    case .Int:
        cell = tableView.dequeueReusableCell(withIdentifier: "valueCell", for: indexPath)
        (cell as! ValueCell).name.text = (mySetting as! IntSetting).name
        (cell as! ValueCell).value.text = String((mySetting as! IntSetting).value)
    case .Float:
        cell = tableView.dequeueReusableCell(withIdentifier: "valueCell", for: indexPath)
        (cell as! ValueCell).name.text = (mySetting as! FloatSetting).name


    }
    return cell
}

//Section címek megadása
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return mySettings.sections[section]
}


}

在 SwitchCell class 中声明一个 属性:

var switchBlock : ((Bool) -> ())? = nil

switchStateChanged 方法的底部执行此操作:

self.switchBlock?(self.value.isOn)

最后在 cellForRow 中替换为:

 (cell as! SwitchCell).switchStateChanged(cell as! SwitchCell.value) {
        (mySetting as! BoolSetting).value = (cell as! SwitchCell).value.isOn
    }

有了这个:

cell.switchBlock = { isOn in (mySetting as! BoolSetting).value = isOn}

盈利!