【发布时间】:2015-06-13 12:48:51
【问题描述】:
这个问题已经在 Objective-C 中处理过here。但我在 Swift 工作并且有一个类似的问题。
创建成功后,如何在点击 UISwitch 时选择 UITableView 所在的行?
我的模型中有一个布尔值,我想根据开关的开/关状态切换该布尔值。
我有一些以编程方式创建的包含开关的单元格...
视图控制器:
var settings : [SettingItem] = [
SettingItem(settingName: "Setting 1", switchState: true),
SettingItem(settingName: "Setting 2", switchState: true)
]
override public func tableView(_tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CustomSettingCell") as! SettingCell
let settingItem = settings[indexPath.row]
cell.settingsLabel.text = settingItem.settingName
cell.settingsSwitch.enabled = settingItem.switchState!
return cell
}
基于 SettingItem.swift 中的模型:
class SettingItem: NSObject {
var settingName : String?
var switchState : Bool?
init (settingName: String?, switchState : Bool?) {
super.init()
self.settingName = settingName
self.switchState = switchState
}
}
我在 SettingCell.swift 中有一些分店:
class SettingCell: UITableViewCell {
@IBOutlet weak var settingsLabel: UILabel!
@IBOutlet weak var settingsSwitch: UISwitch!
@IBAction func handledSwitchChange(sender: UISwitch) {
println("switched")
}
产生这个(请忽略格式):
【问题讨论】:
-
我在这里stackoverflow.com/questions/29354969/…回答了一个类似的问题。希望对您有所帮助。
-
@VictorSigler 我会看看你的帖子,谢谢 Victor。看起来我在发布前搜索时错过了这个。
标签: ios iphone uitableview swift uiswitch