I have a UISwitch
in a tableviewcontroller, and when the switch is toggled I want it to change the value of a boolean variable in an array I created inside the view controller, that the cell is related to. Kind of like the Stock Alarm App on IOS, where each cell has a UISwitch
, and toggling the switch will turn off each individual alarm. So with the UISwitch
, with its selector code, this is inside the cellForRowAtIndexPath
method
//switch
let lightSwitch = UISwitch(frame: CGRectZero) as UISwitch
lightSwitch.on = false
lightSwitch.addTarget(self, action: #selector(switchTriggered), forControlEvents: .ValueChanged)
//lightSwitch.addTarget(self, action: "switchTriggered", forControlEvents: .ValueChanged )
cell.accessoryView = lightSwitch
I want it to do this
func switchTriggered(a: Int) {
changeValueOfArray = array[indexPath.row]
}
I don't have the code written for that part yet, but my question is, How can i let the switchTriggered function see the indexPath.row
value, without passing it as an argument to the function because I can't because its a selector?
The basic idea is that you can capture the cell for which the switch was flipped and then use
tableView.indexPath(for:)
to translate thatUITableViewCell
reference into aNSIndexPath
, and you can use itsrow
to identify which row in your model structure needs to be updated.The constituent elements of this consist of:
Create a model object that captures the information to be shown in the table view. For example, let's imagine that every cell contains a
name
of aRoom
and a boolean reflecting whether the light is on:Then the table view controller would have an array of those:
I'd define a
UITableViewCell
subclass with outlets for the label and the switch. I'd also hook up the "value changed" for the light switch to a method in that cell. I'd also set up a protocol for the cell to inform its table view controller that the light switch was flipped:I'd obviously set the base class for the cell prototype to be this
UITableViewCell
subclass and hook up the@IBOutlet
references as well as the@IBAction
for the changing of the value for the switch.I'd then have the
UITableViewDataSource
methods populate the cell on the basis of theRoom
properties:Note, the above
cellForRowAtIndexPath
also specifies itself as thedelegate
for the cell, so we'd want to implement theRoomLightDelegate
protocol to update our model when the light switch is flipped:Now, I don't want you to worry about the details of the above. Instead, try to capture some of the basic ideas:
Bottom line, to your immediate question, once you know which cell was was updated, you can inquire with the
UITableView
to determine whatNSIndexPath
thatUITableViewCell
reference corresponds to, usingtableView.indexPath(for:)
.Swift 3 Update:
Let save your boolean value in Array