【问题标题】:Update the state of a UISwitch when pressed in a TableView在 TableView 中按下时更新 UISwitch 的状态
【发布时间】: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


    【解决方案1】:

    处理此问题的一种方法是将回调属性添加到RelayCell 并从switchChangedState 调用回调:

    class RelayCell: UITableViewCell {
    
        typealias SwitchCallback = (Bool) -> Void
        var switchCallback: SwitchCallback?
    
        @IBAction func switchChangedState(sender: UISwitch) {
            switchCallback?(sender.on)
        }
    
        // ... rest of RelayCell
    }
    

    在您的tableView:cellForRowAtIndexPath: 方法中,设置单元格的回调:

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell:RelayCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as RelayCell
    
        cell.switchCallback = { [weak self] (switchIsOn) in
            self?.setSwitchValue(switchIsOn, forRowAtIndexPath:indexPath)
            Void()
        }
    
        // ... rest of tableView:cellForRowAtIndexPath:
        return cell
    }
    

    然后你可以在setSwitchValue:forRowAtIndexPath:中做任何你需要的事情,这是你添加到你的表格视图控制器类的方法:

    private func setSwitchValue(switchIsOn: Bool, forRowAtIndexPath indexPath: NSIndexPath) {
        println("row \(indexPath.row) switch on-ness is now \(switchIsOn)")
    }
    

    【讨论】:

      【解决方案2】:

      UISwitch 有“on”属性,用它来获取当前状态的布尔值。

      【讨论】:

      • 我想知道如何使用 tableViewController 类中的 IBaction 函数。我知道 UISwitch 有一个 on 属性并使用它来告诉我开关打开的行并打印它。我想打印按下时改变状态的开关行。
      • “代表”是你的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-17
      • 2019-08-26
      • 1970-01-01
      相关资源
      最近更新 更多