【发布时间】:2015-02-12 18:01:01
【问题描述】:
我有一个需要远程打开/关闭灯的 iOS 应用程序。该应用程序从 parse.com 获取灯光数据,并构建一个表格视图,其中每个单元格显示灯光名称和一个 UISwitch。我想知道当我打开或关闭其中一盏灯时如何更改存储在 parse.com 上的布尔值。问题是开关使用的 IBAction 不是布尔值,我无法编写,如果更新灯值的语句是按下开关。我已经在我的单元格类中创建了 IBaction,并希望 tableviewcontroller 类可以使用它。
这是我的 tableviewcontroller 类的一部分
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:RelayCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as RelayCell
let label:PFObject = self.labelArray.objectAtIndex(indexPath.row) as PFObject //create the object label
cell.relayTextField.text = label.objectForKey("text") as String //put the text in the labeltextField
if (label.objectForKey("switch") as NSObject == 1) {
//cell.mySwitch = true //turn the switch on depending on the boolean value in switchColumn
cell.mySwitch.setOn(true, animated: true)
}
else{
//cell.mySwitch = false //turn the switch on depending on the boolean value in switchColumn
cell.mySwitch.setOn(false, animated: true)
}
return cell
}
此代码显示了每个独立开关的状态,但是,我现在想要的是能够按下应用程序上的每个独立按钮并更改在线数据库上的值。
你能帮我吗,因为我没有在网上找到任何东西。
class RelayCell: UITableViewCell {
@IBOutlet weak var mySwitch: UISwitch!
@IBOutlet weak var relayTextField: UITextField!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
relayTextField.layer.borderColor = UIColor.blackColor().CGColor
relayTextField.layer.borderWidth = 0.8
relayTextField.layer.cornerRadius = 10
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
@IBAction func switchChangedState(sender: UISwitch) {
}
}
这是我的 RelayCell 类,由 tableViewController 类中的 tableView 方法使用。
【问题讨论】:
标签: ios uitableview swift uiswitch