mercredi 6 mai 2015

How do I write to data in the TableView from a custom UITableViewCell

I have a TableView that populates “Characteristic” data from HomeKit. The TableView uses a custom UITableViewCell. The cell has a label and a switch. The cell @IBOutlet and @IBAction work properly allowing me to view the label and toggle/animate the switch in all cells. However, I need the switch to write to the Data Source in the TableView and I do not know how to reference/access the data from the TableView in the custom cell. I have determined how to get the indexPath of the selected cell.

I know there is a simple answer here ….. please help this iOS/XCode/Swift beginner.

import UIKit import HomeKit

class CellServicesCharacteristics: UITableViewCell {

@IBOutlet weak var label2: UILabel!
@IBOutlet weak var switch1: UISwitch!

@IBAction func switch1Act(sender: UISwitch) {

    var cell = sender.superview?.superview as! CellServicesCharacteristics
    var tableView = cell.superview?.superview as! UITableView
    let indexPath = tableView.indexPathForCell(cell) as NSIndexPath!

    // How do I reference the Data Source from the TableView
    // so I can write to the Data Source?

    if switch1.on {
        label2.text = "On"
        // write to data source in tableview
    } else {
        label2.text = "Off"
        // write to data source in tableview
    }

}

override func awakeFromNib() {
super.awakeFromNib()

//Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

// Configure the view for the selected state
}

}

Aucun commentaire:

Enregistrer un commentaire