Programmatically change the state of UISwitch

I have a UITableView with some cell that contains UISwitches:

UISwitch* actSwitch = (UISwitch*)[cell viewWithTag: SWITCH_TAG];
[actSwitch addTarget: self
              action: @selector(actSwitchChanged:) 
    forControlEvents: UIControlEventValueChanged];
BOOL value = [appSettings.lock_when_inactive boolValue];
[actSwitch setOn: value animated:NO];

And I also want to overwrite the method didSelectRowAtIndexPath:to perform the switch of the corresponding UISwitch:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *cellType = [self typeOfCellForIndexPath:indexPath];
    if ( cellType  == @"CellWithSwitch" )
    {
        UISwitch* actSwitch = (UISwitch*)[[[self tableView] cellForRowAtIndexPath:indexPath ] viewWithTag: SWITCH_TAG];
        BOOL value = [actSwitch isOn];
        [actSwitch setOn:!value animated:YES]; // <-- HERE IS THE PROBLEM
        [self actSwitchChanged: actSwitch];
    }
    else if ( cellType == @"CellWithoutSwitch" )
    {
        // other actions
    }
}

In both situations, either I click on UISwitch directly, or by clicking on a cell, it changes its state and calls actSwitchChanged:correctly.

But if I click on a cell, my UISwitch does not animate the switching from one state to another, it just changes its state in one moment.

So what is [actSwitch setOn:!value animated:YES]not enough to say to perforate the animation?


Here, as I install and call the configuration cell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = nil;
    NSString *EnabledCellIdentifier = [self typeOfCellForIndexPath:indexPath];  
    cell = [tableView dequeueReusableCellWithIdentifier:EnabledCellIdentifier];

    if (cell == nil) 
    {
        cell = [self cellForType:EnabledCellIdentifier];
    }
    [self cofigureCell:cell forIndexPath:indexPath];

    return cell;
}

This is how I set up the cell:

- (void)cofigureCell:(UITableViewCell*)cell forIndexPath:(NSIndexPath*)indexPath {
    switch ( indexPath.section )
    {
        case 0: 
            // not this
        case 1: 
        {
            switch ( indexPath.row )
            {
                case 0:
                {
                    cell.textLabel.text = @"Limit passwords attempts";
                    UISwitch* actSwitch = (UISwitch*)[cell viewWithTag: SWITCH_TAG];
                    [actSwitch addTarget: self
                                  action: @selector(actSwitchChanged:) 
                        forControlEvents: UIControlEventValueChanged];
                    BOOL value = [appSettings.limit_password_attempts boolValue];
                    [actSwitch setOn: value animated:NO];

                    break;
                }
                //other rows of this section here are being configured
            }
            break;
        }

        case 2: 
        {
            switch ( indexPath.row )
            {
                case 0:
                {
                    cell.textLabel.text = @"Lock when inactive";
                    UISwitch* actSwitch = (UISwitch*)[cell viewWithTag: SWITCH_TAG];
                    [actSwitch addTarget: self
                                  action: @selector(actSwitchChanged:) 
                        forControlEvents: UIControlEventValueChanged];
                    BOOL value = [appSettings.lock_when_inactive boolValue];
                    [actSwitch setOn: value animated:NO];

                    break;
                }
                //other rows of this section here are being configured
            }
            break;
        }
        default:

            break;
    }
}

But when I debug step by step and go through this step:

[actSwitch setOn:!value animated:YES]; // <-- HERE IS THE PROBLEM

ActSwitch [self actSwitchChanged: actSwitch]; [self.tableView reloadData];

, , UISwitches, ? , UISwitch , ?:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *cellType = [self typeOfCellForIndexPath:indexPath];
    if ( cellType  == @"CellWithSwitch" )
    {
        UISwitch* actSwitch = (UISwitch*)[[[self tableView] cellForRowAtIndexPath:indexPath ] viewWithTag: SWITCH_TAG];
        BOOL value = [actSwitch isOn];
        [actSwitch setOn:!value animated:YES]; // <-- HERE IS THE PROBLEM
        [self actSwitchChanged: actSwitch];
    }
    else if ( cellType == @"CellWithoutSwitch" )
    {
        // other actions
    }
}
+5
2

actSwitchChanged :

UISwitch* actSwitch = (UISwitch*)[[[self tableView] cellForRowAtIndexPath:indexPath ]  viewWithTag: SWITCH_TAG];
    BOOL value = [actSwitch isOn];
    [actSwitch setOn:!value animated:YES]; // <-- HERE IS THE PROBLEM
[self performSelector:@selector(actSwitchChanged:) withObject:actSwitch afterDelay:0.55];
+5

SWIFT 4

UITableViewCell . , :

protocol CellWithSwitchDelegate: class {
    func didToggleSwitch(on: Bool)
}

class CellWithSwitchTableViewCell: UITableViewCell {

    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var toggleSwitch: UISwitch!
    weak var delegate: CellWithSwitchDelegate?

    override func awakeFromNib() {
        super.awakeFromNib()
    }

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

    @IBAction func switchWasToggled(_ sender: UISwitch) {
        delegate?.didToggleSwitch(on: sender.isOn)
    }    

}

UITableViewController :

class UITableViewController: CellWithSwitchDelegate {

    func didToggleSwitch(on: Bool) {
        // This is the delegate function that called when the switch is toggled
        print("toggled switch")
    }
}

UITableViewController didSelectRowAtIndexPath:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath) as! CellWithSwitchTableViewCell
    let settingsSwitch = cell.toggleSwitch!
    didToggleSwitch(on: !settingsSwitch.isOn)
    settingsSwitch.setOn(!settingsSwitch.isOn, animated: true)
}
0

All Articles