2017-03-18 99 views
1

我刚开始学习Xcode和Swift,这是我在堆栈溢出中的第一个问题。所以请原谅我,如果我问一些微不足道的东西,或者我的问题是以不恰当的方式定义的。UITableView自定义单元格选项无法修改源变量

所以我的问题是以下几点: 我想创建一个设置视图,使用自定义单元格的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] 
} 


} 
在SwitchCell类

回答

0

声明一个属性:在switchStateChanged方法底部

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

做到这一点:

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} 

利润!

+0

亲爱的安德烈,感谢您的快速回复!它工作得很好。 –