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

时间:2017-03-18 07:50:55

标签: ios swift uitableview uiswitch

我刚开始学习Xcode和swift,这是我在堆栈溢出时的第一个问题。所以请原谅我,如果我问一些微不足道的事情,或者我的问题是以不恰当的方式定义的。

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

提前感谢您的帮助。

我的SwitchCell类:

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 BoolSetting: Setting {
 var value: Bool
 init (name: String, value: Bool) {
    self.value = value
    super.init(name: name, type: .Bool)
 }
}

我的SettingsListItems类:

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类:

 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]
}


}

1 个答案:

答案 0 :(得分:0)

SwitchCell类中的

声明了一个属性:

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}

利润!