title: Button pressed in Table View Cell with reusable cells
body: <p>I'm struggling with the following problem.</p>
  
  <p>I've got a table view of options for a boat. Each option (cell) has a name, a price and a button that indicates whether the option has been selected or not (see attached screenshot)-</p>
  
  <p><img src="https://i.stack.imgur.com/UrPuk.png" alt="enter image description here"></p>
  
  <p>The problem is that when I tap on a couple of options from Section 1, I get the button_pressed state also for cells in the last section. Probably this happens because cells are reused ?</p>
  
  <p>This is the function that gets called when the cell is tapped, to add the option.</p>
  
  <pre><code>- (IBAction)cellButtonSelection:(id)sender {
  
  float prezzoParziale;
  
  UIButton * button = sender;
  //NSLog(@"%@",button.superview.superview);
  if ([button.superview.superview isKindOfClass:[CellOptions class]] ) {
  
      CellOptions * cellOpt =(CellOptions *)button.superview.superview;
  
      //BOOL selectionState = NO;
      cellOpt.selected =! cellOpt.selected;
  
      if(cellOpt.selected){
          //UIButton *button = cellOpt.button1;
          [button setImage:[UIImage imageNamed:@"checkedButton.png"] forState:UIControlStateNormal];
          prezzoParziale = [cellOpt.label2.text floatValue];
          [accessoriSelezionati addObject:cellOpt.label1.text];
          [accessoriSelezionatiPrezzi addObject: cellOpt.label2.text];
  
          NSLog(@"stampa: %@", accessoriSelezionati);
  
  
      }else{
          //UIButton *button = cellOpt.button1;
          [button setImage:[UIImage imageNamed:@"uncheckedButton.png"] forState:UIControlStateNormal];
          prezzoParziale = [cellOpt.label2.text floatValue] * -1;
          [accessoriSelezionati removeObject:cellOpt.label1.text];
          [accessoriSelezionatiPrezzi removeObject: cellOpt.label2.text];
  
      }
  
  
   [self updateTotal:prezzoParziale]; }
  </code></pre>
  
  <p>And.....</p>
  
  <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
  
          CellOptions * cellOpt;
       cellOpt = [tableView dequeueReusableCellWithIdentifier:CellOption forIndexPath:indexPath];
       cellOpt.label1.text = [[[accessoriXtipologia objectForKey:tipologia] objectAtIndex:indexPath.row] valueForKey:@"nome"];
      cellOpt.label2.text = [[[[accessoriXtipologia objectForKey:tipologia] objectAtIndex:indexPath.row] valueForKey:@"prezzo"] stringValue];
  
      return cellOpt;
  
      }
  </code></pre>
  
  <p>EDIT: Now it's working</p>
  
  <p>Added in cellForRowAtIndexPath:</p>
  
  <pre><code>cellOpt.selected = [accessoriSelezionati containsObject:cellOpt.label1.text];
  
  
  
              if (cellOpt.selected) {
  
                  [cellOpt.button1 setImage:[UIImage imageNamed:@"checkedButton.png"] forState:UIControlStateNormal];
              }
  
              else {
  
  
  
              [cellOpt.button1 setImage:[UIImage imageNamed:@"uncheckedButton.png"] forState:UIControlStateNormal];
  
              }
  
              return cellOpt;
  </code></pre>