Swift: How can I catch a modification of a switch from dynamic cell

强颜欢笑 提交于 2019-12-12 06:27:56

问题


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
    }

    }

回答1:


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.



来源:https://stackoverflow.com/questions/38321372/swift-how-can-i-catch-a-modification-of-a-switch-from-dynamic-cell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!