title: UIButton reused in UITableViewCell when scrolling
body: <p>In my UITableView, there are header cells and standard cells. Each standard cell has a check box (a <code>UIButton</code>) added to it, using the following two methods.  </p>
  
  <p><strong>Method below is called to create each <code>UITableViewCell</code>:</strong></p>
  
  <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
  
  static NSString *tableIdentifier = @"activityIdentifier";
  
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier];
  
  // HEADER ROWS
  if (indexPath.row == 0 || indexPath.row == 5 || indexPath.row == 10 || indexPath.row == 15 || indexPath.row == 20 || indexPath.row == 25 || indexPath.row == 30) {
  
      if (cell == nil) {
          cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier];
          NSLog(@"Yeaa!");
      }
  
      cell.textLabel.text = [activityArray objectAtIndex:indexPath.row];
      cell.textLabel.textColor = [UIColor blackColor];
      cell.textLabel.font = [UIFont fontWithName:@"Aliquam" size:13.0f];
      cell.selectionStyle = UITableViewCellSelectionStyleNone;
      cell.backgroundColor = [UIColor whiteColor];
      static int a = 20.0f;
      tableView.rowHeight = a;
  
  } else {
      // STANDARD ROWS
      if (cell == nil) {
          cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier];
          NSLog(@"Standard cell is nil");
  
      }
  
      cell.textLabel.text = [activityArray objectAtIndex:indexPath.row];
      cell.backgroundColor = [UIColor clearColor];
      cell.textLabel.textColor = [UIColor whiteColor];
      cell.textLabel.font = [UIFont fontWithName:@"Aliquam" size:20.0f];
      cell.selectionStyle = UITableViewCellSelectionStyleNone;
      static int b = 44.0f;
      tableView.rowHeight = b;
  }
  
  // CREATE CHECK BOXES IN STANDARD ROWS
  if (indexPath.row == 1) {
      [self createRoleCheckbox:actMondayMorning];
      [cell addSubview:actMondayMorning];
  } else if (indexPath.row == 2) {
      [self createRoleCheckbox:actMondayAfternoon];
      [cell addSubview:actMondayAfternoon];
  } else if (indexPath.row == 3) {
      [self createRoleCheckbox:actMondayEvening];
      [cell addSubview:actMondayEvening];
  } else if (indexPath.row == 4) {
      [self createRoleCheckbox:actMondayNight];
      [cell addSubview:actMondayNight];
  } else if (indexPath.row == 6) {
      [self createRoleCheckbox:actTuesdayMorning];
      [cell addSubview:actTuesdayMorning];
  } else if (indexPath.row == 7) {
      [self createRoleCheckbox:actTuesdayAfternoon];
      [cell addSubview:actTuesdayAfternoon];
  } else if (indexPath.row == 8) {
      [self createRoleCheckbox:actTuesdayEvening];
      [cell addSubview:actTuesdayEvening];
  } else if (indexPath.row == 9) {
      [self createRoleCheckbox:actTuesdayNight];
      [cell addSubview:actTuesdayNight];
  } else if (indexPath.row == 11) {
      [self createRoleCheckbox:actWednesdayMorning];
      [cell addSubview:actWednesdayMorning];
  } else if (indexPath.row == 12) {
      [self createRoleCheckbox:actWednesdayAfternoon];
      [cell addSubview:actWednesdayAfternoon];
  } else if (indexPath.row == 13) {
      [self createRoleCheckbox:actWednesdayEvening];
      [cell addSubview:actWednesdayEvening];
  } else if (indexPath.row == 14) {
      [self createRoleCheckbox:actWednesdayNight];
      [cell addSubview:actWednesdayNight];
  } else if (indexPath.row == 16) {
      [self createRoleCheckbox:actThursdayMorning];
      [cell addSubview:actThursdayMorning];
  } else if (indexPath.row == 17) {
      [self createRoleCheckbox:actThursdayAfternoon];
      [cell addSubview:actThursdayAfternoon];
  } else if (indexPath.row == 18) {
      [self createRoleCheckbox:actThursdayEvening];
      [cell addSubview:actThursdayEvening];
  } else if (indexPath.row == 19) {
      [self createRoleCheckbox:actThursdayNight];
      [cell addSubview:actThursdayNight];
  } else if (indexPath.row == 21) {
      [self createRoleCheckbox:actFridayMorning];
      [cell addSubview:actFridayMorning];
  } else if (indexPath.row == 22) {
      [self createRoleCheckbox:actFridayAfternoon];
      [cell addSubview:actFridayAfternoon];
  } else if (indexPath.row == 23) {
      [self createRoleCheckbox:actFridayEvening];
      [cell addSubview:actFridayEvening];
  } else if (indexPath.row == 24) {
      [self createRoleCheckbox:actFridayNight];
      [cell addSubview:actFridayNight];
  } else if (indexPath.row == 26) {
      [self createRoleCheckbox:actSaturdayMorning];
      [cell addSubview:actSaturdayMorning];
  } else if (indexPath.row == 27) {
      [self createRoleCheckbox:actSaturdayAfternoon];
      [cell addSubview:actSaturdayAfternoon];
  } else if (indexPath.row == 28) {
      [self createRoleCheckbox:actSaturdayEvening];
      [cell addSubview:actSaturdayEvening];
  } else if (indexPath.row == 29) {
      [self createRoleCheckbox:actSaturdayNight];
      [cell addSubview:actSaturdayNight];
  } else if (indexPath.row == 31) {
      [self createRoleCheckbox:actSundayMorning];
      [cell addSubview:actSundayMorning];
  } else if (indexPath.row == 32) {
      [self createRoleCheckbox:actSundayAfternoon];
      [cell addSubview:actSundayAfternoon];
  } else if (indexPath.row == 33) {
      [self createRoleCheckbox:actSundayEvening];
      [cell addSubview:actSundayEvening];
  } else if (indexPath.row == 34) {
      [self createRoleCheckbox:actSundayNight];
      [cell addSubview:actSundayNight];
  }
  
  return cell;
  }
  </code></pre>
  
  <p><strong>Method below is called to create the checkboxes (<code>UIButton</code>):</strong></p>
  
  <pre><code>- (void) createRoleCheckbox:(UIButton *)passedButton {
  
  passedButton.frame = CGRectMake(screenWidth-checkboxDiameter-(checkboxInset/2), (checkboxInset/2), checkboxDiameter, checkboxDiameter);
  
  [passedButton setImage:checkboxImageUnselected forState:UIControlStateNormal];
  [passedButton setImage:checkboxImageSelected forState:UIControlStateSelected];
  
  if (passedButton == actMondayMorning)
      [passedButton addTarget:self action:@selector(actMondayMorningPressed) forControlEvents:UIControlEventTouchUpInside];
  else if (passedButton == actMondayAfternoon)
      [passedButton addTarget:self action:@selector(actMondayAfternoonPressed) forControlEvents:UIControlEventTouchUpInside];
  else if (passedButton == actMondayEvening)
      [passedButton addTarget:self action:@selector(actMondayEveningPressed) forControlEvents:UIControlEventTouchUpInside];
  else if (passedButton == actMondayNight)
      [passedButton addTarget:self action:@selector(actMondayNightPressed) forControlEvents:UIControlEventTouchUpInside];
  else if (passedButton == actTuesdayMorning)
      [passedButton addTarget:self action:@selector(actTuesdayMorningPressed) forControlEvents:UIControlEventTouchUpInside];
  else if (passedButton == actTuesdayAfternoon)
      [passedButton addTarget:self action:@selector(actTuesdayAfternoonPressed) forControlEvents:UIControlEventTouchUpInside];
  else if (passedButton == actTuesdayEvening)
      [passedButton addTarget:self action:@selector(actTuesdayEveningPressed) forControlEvents:UIControlEventTouchUpInside];
  else if (passedButton == actTuesdayNight)
      [passedButton addTarget:self action:@selector(actTuesdayNightPressed) forControlEvents:UIControlEventTouchUpInside];
  else if (passedButton == actWednesdayMorning)
      [passedButton addTarget:self action:@selector(actWednesdayMorningPressed) forControlEvents:UIControlEventTouchUpInside];
  else if (passedButton == actWednesdayAfternoon)
      [passedButton addTarget:self action:@selector(actWednesdayAfternoonPressed) forControlEvents:UIControlEventTouchUpInside];
  else if (passedButton == actWednesdayEvening)
      [passedButton addTarget:self action:@selector(actWednesdayEveningPressed) forControlEvents:UIControlEventTouchUpInside];
  else if (passedButton == actWednesdayNight)
      [passedButton addTarget:self action:@selector(actWednesdayNightPressed) forControlEvents:UIControlEventTouchUpInside];
  else if (passedButton == actThursdayMorning)
      [passedButton addTarget:self action:@selector(actThursdayMorningPressed) forControlEvents:UIControlEventTouchUpInside];
  else if (passedButton == actThursdayAfternoon)
      [passedButton addTarget:self action:@selector(actThursdayAfternoonPressed) forControlEvents:UIControlEventTouchUpInside];
  else if (passedButton == actThursdayEvening)
      [passedButton addTarget:self action:@selector(actThursdayEveningPressed) forControlEvents:UIControlEventTouchUpInside];
  else if (passedButton == actThursdayNight)
      [passedButton addTarget:self action:@selector(actThursdayNightPressed) forControlEvents:UIControlEventTouchUpInside];
  else if (passedButton == actFridayMorning)
      [passedButton addTarget:self action:@selector(actFridayMorningPressed) forControlEvents:UIControlEventTouchUpInside];
  else if (passedButton == actFridayAfternoon)
      [passedButton addTarget:self action:@selector(actFridayAfternoonPressed) forControlEvents:UIControlEventTouchUpInside];
  else if (passedButton == actFridayEvening)
      [passedButton addTarget:self action:@selector(actFridayEveningPressed) forControlEvents:UIControlEventTouchUpInside];
  else if (passedButton == actFridayNight)
      [passedButton addTarget:self action:@selector(actFridayNightPressed) forControlEvents:UIControlEventTouchUpInside];
  else if (passedButton == actSaturdayMorning)
      [passedButton addTarget:self action:@selector(actSaturdayMorningPressed) forControlEvents:UIControlEventTouchUpInside];
  else if (passedButton == actSaturdayAfternoon)
      [passedButton addTarget:self action:@selector(actSaturdayAfternoonPressed) forControlEvents:UIControlEventTouchUpInside];
  else if (passedButton == actSaturdayEvening)
      [passedButton addTarget:self action:@selector(actSaturdayEveningPressed) forControlEvents:UIControlEventTouchUpInside];
  else if (passedButton == actSaturdayNight)
      [passedButton addTarget:self action:@selector(actSaturdayNightPressed) forControlEvents:UIControlEventTouchUpInside];
  else if (passedButton == actSundayMorning)
      [passedButton addTarget:self action:@selector(actSundayMorningPressed) forControlEvents:UIControlEventTouchUpInside];
  else if (passedButton == actSundayAfternoon)
      [passedButton addTarget:self action:@selector(actSundayAfternoonPressed) forControlEvents:UIControlEventTouchUpInside];
  else if (passedButton == actSundayEvening)
      [passedButton addTarget:self action:@selector(actSundayEveningPressed) forControlEvents:UIControlEventTouchUpInside];
  else if (passedButton == actSundayNight)
      [passedButton addTarget:self action:@selector(actSundayNightPressed) forControlEvents:UIControlEventTouchUpInside];
  
  }
  </code></pre>
  
  <p>This code correctly creates check boxes in each row.</p>
  
  <p>However, when I scroll through my app, the <code>UIButton</code>s duplicate, randomly appearing in the header cells.</p>
  
  <p>In addition, if I click a button, putting it in its selected state, when I scroll, other buttons will randomly also be selected state also, even when I did not press them.  </p>
  
  <p>In short, the <code>UIButton</code>s are being reused. How can I this from happening?  </p>
  
  <p>All help appreciated.</p>