Swift:如何从动态单元获取交换机的修改 [英] Swift: How can I catch a modification of a switch from dynamic cell

查看:148
本文介绍了Swift:如何从动态单元获取交换机的修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要抓住动态单元格中的交换机的修改。
该应用程序有动态单元格列出所有找到的所有BLE设备。



BLE设备应该能够通过开关打开/关闭。 >

现在是问题,我无法通过Outlet或Action来抓住切换。



如何捕捉从动态单元格更改。



UI的问题看起来像这样:
具有动态单元格的UI



我发现了我可以抓住水龙头的点,但是我不能使用我的方法,因为在类类型UITableViewCell CoreBluetooth不支持,我无法填写CoreBluetooth方法的属性。

  import UIKit 


class remoteCellTableViewCell:UITableViewCell {

// MARK:属性
@IBOutlet weak var remoteCellLabel:UILabel!
@IBOutlet weak var remoteCellImageView:UIImageView!
@IBOutlet weak var remoteCellSwitch:UISwitch!
@IBOutlet weak var remoteCellPower:UIProgressView!

覆盖func awakeFromNib(){
super.awakeFromNib()

}

//这里我可以捕获开关TAP我不能使用CoreBluetooh在这个类
@IBAction func onOff(发件人:AnyObject){


//对象从类的连接和发送到外部
var connection:ViewController = ViewController()

connection.viewDidLoad()


// CANT FILLOUT属性由于丢失COREBLUETOOTH支持
// - >
connection.centralManagerDidUpdateState(<#T ## central:CBCentralManager ## CBCentralManager#>)
connection.centralManager(<#T ## central:CBCentralManager ## CBCentralManager#> didDiscoverPeripheral: <#T ## CBPeripheral#>,advertisingData:< #T ## [String:AnyObject]#>,RSSI:<#T ## NSNumber#>)
connection.centralManager(< ;#T ## central:CBCentralManager ## CBCentralManager#> didConnectPeripheral:<#T ## CBPeripheral#>)
connection.centralManager(<#T ## central:CBCentralManager ## CBCentralManager#> ;,didDiscoverPeripheral:<#T ## CBPeripheral#>,advertisingData:< #T ## [String:AnyObject]#>,RSSI:<#T ## NSNumber#>)
连接.centralManager(<#T ## central:CBCentralManager ## CBCentralManager#>,didConnectPeripheral:<#T ## CBPeripheral#>)
connection.peripheral(<#T ## peripheral:CBPeripheral# #CBPeripheral#> didDiscoverServices:<#T ## NSError?#>)
connection.peripheral(<#T ## peripheral:CBPeripheral ## CBPeripheral#>,didDiscoverCharacteristicsForService:<#T ## CBService#>,error:<#T ## NSError?#>)
connection.peripheral(<#T ## peripheral:CBPeripheral ## CBPeripheral#>,didUpdateValueForCharacteristic:<#T ## CBCharacteristic#>,error:<#T ## NSError?#>)
connection.peripheral(<#T ## peripheral:CBPeripheral ## CBPeripheral#>,didWriteValueForDescriptor:<#T ##CB描述符#>,错误:<#T ## NSError?#>)

// - >
}
覆盖func setSelected(selected:Bool,animated:Bool){
super.setSelected(selected,animated:animated)

//配置视图所选状态
}

}


解决方案

使用标签获取开关状态。这里是我用来获取按钮点击动作的代码。像这样



在您的 cellForRowAtIndexPath 中分配标签到按钮。

  cell.absentButton.tag = indexPath.row 

在你的按钮操作获取按钮的行

 如果let button =发件人为? UIButton {
如果button.selected {
//设置选择
let row = sender.tag
}
}

从这里你可以得到哪个开关被改变。


I need to catch the modification of a switch in a dynamic cell. The app has dynamic cells to list all BLE devices which where found.

The BLE device should be able to turn on/off via switch.

Now is the problem, I can't catch the switch via Outlet or Action.

How is it possible to catch a change from a dynamic cell.

UI for the problem looks like that: UI with dynamic cell

I found the point where I can catch the tap but there I can't use my methods because in a class type UITableViewCell CoreBluetooth is not supported and I can't fill out the attribute for the CoreBluetooth methods.

import UIKit


class remoteCellTableViewCell: UITableViewCell{

    //MARK: Properties
    @IBOutlet weak var remoteCellLabel: UILabel!
    @IBOutlet weak var remoteCellImageView: UIImageView!
    @IBOutlet weak var remoteCellSwitch: UISwitch!
    @IBOutlet weak var remoteCellPower: UIProgressView!

    override func awakeFromNib() {
        super.awakeFromNib()

    }

    //HERE I CAN CATCH THE SWITCH TAP BUT I CAN'T USE CoreBluetooh IN THIS CLASS
    @IBAction func onOff(sender: AnyObject) {


        //OBJECT FROM THE CLASS FOR CONNECT AND SEND TO THE PERIPHERAL
        var connection: ViewController = ViewController()

        connection.viewDidLoad()


        //CANT FILLOUT THE ATTRIBUTES BECAUSE OF MISSING COREBLUETOOTH SUPPORT
    //-->        
        connection.centralManagerDidUpdateState(<#T##central: CBCentralManager##CBCentralManager#>)
        connection.centralManager(<#T##central: CBCentralManager##CBCentralManager#>, didDiscoverPeripheral: <#T##CBPeripheral#>, advertisementData: <#T##[String : AnyObject]#>, RSSI: <#T##NSNumber#>)
        connection.centralManager(<#T##central: CBCentralManager##CBCentralManager#>, didConnectPeripheral: <#T##CBPeripheral#>)
        connection.centralManager(<#T##central: CBCentralManager##CBCentralManager#>, didDiscoverPeripheral: <#T##CBPeripheral#>, advertisementData: <#T##[String : AnyObject]#>, RSSI: <#T##NSNumber#>)
        connection.centralManager(<#T##central: CBCentralManager##CBCentralManager#>, didConnectPeripheral: <#T##CBPeripheral#>)
        connection.peripheral(<#T##peripheral: CBPeripheral##CBPeripheral#>, didDiscoverServices: <#T##NSError?#>)
        connection.peripheral(<#T##peripheral: CBPeripheral##CBPeripheral#>, didDiscoverCharacteristicsForService: <#T##CBService#>, error: <#T##NSError?#>)
        connection.peripheral(<#T##peripheral: CBPeripheral##CBPeripheral#>, didUpdateValueForCharacteristic: <#T##CBCharacteristic#>, error: <#T##NSError?#>)
        connection.peripheral(<#T##peripheral: CBPeripheral##CBPeripheral#>, didWriteValueForDescriptor: <#T##CBDescriptor#>, error: <#T##NSError?#>)

    //->
    }
    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    }

解决方案

Use Tags to get the switch state. here is the code that i used to get the button click action. Like this

In your cellForRowAtIndexPath assign tag to the button.

cell.absentButton.tag = indexPath.row

In your button action get the row of the button

if let button = sender as? UIButton {
  if button.selected {
    // set selected
    let row = sender.tag
 }
}

From this you can get which switch is changed.

这篇关于Swift:如何从动态单元获取交换机的修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆