title: Save pressed button state UITableViewCell
body: <p>I've <code>UITableView</code> with dynamic cells and the cells are objects of custom <code>UITableViewCell</code> subclass.
  Each cell contains <code>UILabel</code>,<code>UIImageView</code> and <code>UIButton</code> inside this <code>UIImageView</code>. For clarity it is a music player which play's stream audio. My problem is saving pressed button state i.e for example when my <code>UITableView</code> first loaded all buttons are in "deselected" state and each button has "play.png" as image, then if I press on this button its image changes to "pause.png" and target change too. It's very obvious, but if I will scroll up or down my <code>UITableView</code> I'll see cells with "pause" button even if I never pressed on these buttons. I know that I must save cell button stae in <code>NSMutableArray</code> and I already do it, but I guess that I do it wrong. Please help me solve this typical problem.</p>
  
  <p>My source:</p>
  
  <p><strong>Play/pause switch method:</strong></p>
  
  <pre><code>-(void)selectSettings:(AVMPlayButton *)sender{
  
  CGPoint pointInTable = [sender convertPoint:sender.bounds.origin toView:musicTable];
  NSIndexPath *indexPath = [musicTable indexPathForRowAtPoint:pointInTable];
  AVMMusicCell *cell=(AVMMusicCell *)[musicTable cellForRowAtIndexPath:indexPath];
  
  NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:(unsigned int)indexPath.row];
  [sender setSelected:!sender.isSelected];
  
  if (sender.isSelected) {
      NSLog(@"SELECTED!");
  
      if (![selectedButonsToPlay containsObject:[NSString stringWithFormat:@"%@",rowNsNum]]  ) // HERE IS ARRAY WHICH CONTAINS SELECTED BUTTONS
      {
          [selectedButonsToPlay addObject:[NSString stringWithFormat:@"%ld",(long)indexPath.row]];
          [sender addTarget:self action:@selector(pausePlay:) forControlEvents:UIControlEventTouchUpInside];
          UIImage *pauseBackground = [UIImage imageNamed:@"pause.png"];
          [sender setImage:pauseBackground forState:UIControlStateSelected];
      }
  
  }else {
      NSLog(@"DESELECTED!");
      if ( [selectedButonsToPlay containsObject:[NSString stringWithFormat:@"%@",rowNsNum]]  )
      {
          [selectedButonsToPlay removeObject:[NSString stringWithFormat:@"%ld",(long)indexPath.row]];
          //    [cell.playButton addTarget:self action:@selector(startPlay:) forControlEvents:UIControlEventTouchUpInside];
          NSLog(@"DEselected in didDEselect");
          }
      }
  
  NSLog(@"you just select button");
  }
  
  
  -(IBAction)pausePlay:(AVMPlayButton*)sender{
  AVMPlayButton *settings = (AVMPlayButton *)sender;
  CGPoint pointInTable = [settings convertPoint:settings.bounds.origin toView:musicTable];
  NSIndexPath *indexPath = [musicTable indexPathForRowAtPoint:pointInTable];
  AVMMusicCell *cell=(AVMMusicCell *)[musicTable cellForRowAtIndexPath:indexPath];
  UIImage *playBackground = [UIImage imageNamed:@"play.png"];
  [cell.playButton setImage:playBackground forState:UIControlStateNormal];
  [self pauseStream];
  
  }
  </code></pre>
  
  <p><strong>CellForRowAtIndexPath method:</strong></p>
  
  <pre><code> - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
     static NSString *CellIdentifier = @"fileCell";
     AVMMusicCell *cell = [self.musicTable dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
      cell = [[AVMMusicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  }
  
  AVMDataStore *oneItem = [musicArray objectAtIndex:indexPath.row];
  oneItem = [musicArray objectAtIndex:indexPath.row];
  
  
  [cell setMusic:oneItem];
  cell.songName.text = oneItem.fileName;
  UIView *bgColorView = [[UIView alloc] init];
  bgColorView.backgroundColor = MINT;
  [cell setSelectedBackgroundView:bgColorView];
  
  [cell.playButton addTarget:self action:@selector(selectSettings:) forControlEvents:UIControlEventTouchUpInside];
  
  //save cell state for selected CELLS
  NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:(unsigned int)indexPath.row];
  
  UIImage *pauseBackground = [UIImage imageNamed:@"pause.png"];
  UIImage *playBackground = [UIImage imageNamed:@"play.png"];
  if ([selectedButonsToPlay containsObject:[NSString stringWithFormat:@"%@",rowNsNum]]  )
  {
      NSLog(@"cellforrow %lu contains selected buttons!",indexPath.row);
    //  NSLog(@"picture in cell %lu button is %@",indexPath.row,cell.playButton.imageView.image);
     // [cell.playButton removeTarget:self action:@selector(selectSettings:) forControlEvents:UIControlEventTouchUpInside];
      [cell.playButton addTarget:self action:@selector(pausePlay:) forControlEvents:UIControlEventTouchUpInside];
  
      [cell.playButton setImage:pauseBackground forState:UIControlStateSelected];
  }
  else{
      NSLog(@"cell %lu does not contain selected buton",indexPath.row);
      [cell.playButton addTarget:self action:@selector(startPlay:) forControlEvents:UIControlEventTouchUpInside];
          [cell.playButton setImage:playBackground forState:UIControlStateNormal];
  }
  
  
  return cell;
  
  
  }
  </code></pre>
  
  <p>How it looks like:</p>
  
  <p><strong>When no one button is pressed:</strong></p>
  
  <p><img src="https://i.stack.imgur.com/dXnQz.png" alt="When no one button is pressed"></p>
  
  <p><strong>Press play button and it's ok</strong></p>
  
  <p><img src="https://i.stack.imgur.com/74LXi.png" alt="Press play button and it&#39;s ok"></p>
  
  <p><strong>Scroll down and see that on the fourth cell after "play" button is pressed too (but actually it's not)</strong></p>
  
  <p><img src="https://i.stack.imgur.com/bfOgK.png" alt="Scroll down and see that on the fourth cell after &quot;play&quot; button is pressed too (but actually it&#39;s not)"></p>
  
  <p><strong>Scroll up and see that selected button state moved on cell near "play" was really pressed</strong>
  <img src="https://i.stack.imgur.com/S5R7t.png" alt="Scroll up and see that selected button state moved on cell near &quot;play&quot; was really pressed"></p>