Spencer Milanak
Spencer Milanak

Reputation: 133

Swift 3 - How can I store the current value of a UISwitch in a UITableViewCell, in a variable on the actual ViewController?

I have a UISwitch that appears in a UITableViewCell, and the functions & values of it are stored in its class. Now, when the switch is changed, it calls a function inside my UITableViewCell class, but from that function, I can't actually change variables on the viewcontroller that it appears on, which is what I need to do.

What I want to do is say, when the switch is changed, change variable trueOrFalse (on viewcontroller), based on the value of the switch. But I can't access the variable trueOrFalse from the custom tableviewcell class, which is where the function is called when the switch changes value.

Does anybody have any ideas on how to do this? Any help would be much appreciated.

Upvotes: 0

Views: 840

Answers (4)

Duncan C
Duncan C

Reputation: 131481

Set up a protocol that lets your cells notify their delegate about user actions like toggling switches.

Have your view controller conform to that protocol. In your cellForRow(at:) method, set the delegate property of the cell to the view controller.

You can have the cell send itself as a sender property in it's delegate methods, and then ask the table view for the indexPath of the cell that sent the message.

Upvotes: 0

The Mach System
The Mach System

Reputation: 6973

I assume that your question was you wished to know which UITableViewCell the UISwitch belongs to when it is switched on/off.

Here is the code to identify the cell whose switch belongs to.

- (UITableViewCell *)cellContainingSwitch:(UISwitch *)switch{
    UITableViewCell *cell = (UITableViewCell *)switch.superview;
    while (![cell isKindOfClass:[UITableViewCell class]]) {
        cell = (UITableViewCell *)cell.superview;
    }
    return cell;
}

So in your action method, do this:

- (IBAction)yourSwitchSwitched:(UISwitch *)sender {
    UITableViewCell *cell = [self cellContainingSwitch:sender];
    // Then do whatever you want with this cell.          
}

Unfortunately, I do not have experience in Swift so this code is written in Objective-C but you get the picture.

Upvotes: -1

azizj
azizj

Reputation: 3787

It's actually not that bad. In cellForRowAt, set a target for the switch:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "switchCell", for: indexPath) as! SwitchContainingTableViewCell
    cell.mySwitch.addTarget(self, action: #selector(switchChanged(_:)), for: .valueChanged)
    return cell
}

func switchChanged(_ mySwitch: UISwitch) {
    self.value = mySwitch.isOn
    // Do something
}

Upvotes: 2

ghostatron
ghostatron

Reputation: 2650

Two options off the top of my head:

  1. In your custom cell class, add a property for "parentViewController" and in your cellForRow method, assign your view controller to that property.

  2. Have your cell blast out a notification, and have your view controller observe for it.

Upvotes: 3

Related Questions