title: Change the state of UISwitch with animation programmatically
body: <p>I have a UITableView with some cell's that containt UISwitches:</p>
  
  <pre><code>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];
  </code></pre>
  
  <p>And I also want to overwrite <code>didSelectRowAtIndexPath:</code> method to perform toggling of respective UISwitch:</p>
  
  <pre><code>- (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]; // &lt;-- HERE IS THE PROBLEM
          [self actSwitchChanged: actSwitch];
      }
      else if ( cellType == @"CellWithoutSwitch" )
      {
          // other actions
      }
  }
  </code></pre>
  
  <p>In both situations, either I click on the UISwitch directly or by clicking on cell, it changes it's state and call's <code>actSwitchChanged:</code> correctly. </p>
  
  <p>But in case if I click on the cell, my UISwitch doesn't animate the flipping from one state to another, it just changes its state in a single moment.</p>
  
  <p>So <code>[actSwitch setOn:!value animated:YES]</code> isn't enough it to tell to perfom animation?</p>
  
  <hr>
  
  <p>Here's how I set &amp; call configure cell:</p>
  
  <pre><code>- (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;
  }
  </code></pre>
  
  <p>This is how I configure cell:</p>
  
  <pre><code>- (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;
      }
  }
  </code></pre>
  
  <p>But when I debug step by step, and go through this step:</p>
  
  <p><code>[actSwitch setOn:!value animated:YES]; // &lt;-- HERE IS THE PROBLEM</code></p>
  
  <p>The actSwitch changes its state only after <code>[self actSwitchChanged: actSwitch];</code> changed data model and calls <code>[self.tableView reloadData];</code></p>
  
  <p>May be the reason is that I have two cells with UISwitches, and there accures some conflict between them?
  May be there is a better way to get UISwitch from the cell then this code of mine?:</p>
  
  <pre><code>- (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]; // &lt;-- HERE IS THE PROBLEM
          [self actSwitchChanged: actSwitch];
      }
      else if ( cellType == @"CellWithoutSwitch" )
      {
          // other actions
      }
  }
  </code></pre>