Ios UITableView自定义单元格选项可以';t修改源变量
我刚开始学习Xcode和swift,这是我在堆栈溢出中的第一个问题。所以请原谅我,如果我问了一些琐碎的问题,或者我的问题定义不恰当 所以我的问题是: 我正在尝试创建一个设置视图,其中TableView使用自定义单元格。 我有两种类型的单元格:ValueCell(带两个标签)和SwitchCell(一个标签和一个开关)。这两个单元我都有课。My UITableViewController包含用于填充tableview的输入变量。经过一番努力,我终于可以做到这一点。因此,我的TableView中填充了我的所有设置。 因此,问题如下: 当我与开关交互时,如何让开关修改原始值 提前感谢您的帮助 我的SwitchCell类:Ios UITableView自定义单元格选项可以';t修改源变量,ios,swift,uitableview,uiswitch,Ios,Swift,Uitableview,Uiswitch,我刚开始学习Xcode和swift,这是我在堆栈溢出中的第一个问题。所以请原谅我,如果我问了一些琐碎的问题,或者我的问题定义不恰当 所以我的问题是: 我正在尝试创建一个设置视图,其中TableView使用自定义单元格。 我有两种类型的单元格:ValueCell(带两个标签)和SwitchCell(一个标签和一个开关)。这两个单元我都有课。My UITableViewController包含用于填充tableview的输入变量。经过一番努力,我终于可以做到这一点。因此,我的TableView中填充
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)
}
}
我的设置系统类:
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类中声明一个属性:
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}
利润 在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}
利润 亲爱的安德烈,谢谢你的快速回复!非常好。亲爱的安德烈,谢谢你的快速回复!它工作得很好。