title: UITableViewCell duplicating UITextfield subviews when scrolling
body: <p>I'm trying to figure out why my <code>Subviews</code> are duplicating when the <code>UITableview</code> scrolls. If I add the <code>Subviews</code> inside the <code>IF statement</code> for reusing the cell it duplicates and reorders the <code>Subviews</code>. If I put the code on the outside it messes up by putting the <code>Subviews</code> on top of one another when scrolling. Below is my code. What am I doing wrong to cause this?</p>
  
  <pre><code>static NSString *cellIdentifier = @"Cell";
  
  UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  
  if (cell == nil) {
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
  
  
  
  
  
  cell.accessoryType = UITableViewCellAccessoryNone;
  
      UIColor *cellBackgroundColor = [rgbConverter convertColor:@"#202e35"];
      cell.backgroundColor = cellBackgroundColor;
      // cell.contentView.backgroundColor    = color;
  
  
      if ((indexPath.row == 0) &amp;&amp; (indexPath.section == 0)) {
          fullName = [[UITextField alloc] initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 200, 25)];
          fullName.font = [UIFont fontWithName:@"HelveticaNeue" size:15];
          fullName.autocorrectionType = UITextAutocorrectionTypeNo;
          [fullName setClearButtonMode:UITextFieldViewModeWhileEditing];
          fullName.textColor  = [UIColor lightGrayColor];
          [fullName setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
          fullName.returnKeyType = UIReturnKeyNext;
          fullName.background = [UIImage imageNamed:@"textfieldBG.png"];
          [fullName setTag:1];
          [fullName setText:@""];
  
          [cell.contentView  addSubview:fullName];
  
      }
      if ((indexPath.row == 1)  &amp;&amp; (indexPath.section == 0)){
          accountNumber = [[UITextField alloc] initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 200, 25)];
          accountNumber.font = [UIFont fontWithName:@"HelveticaNeue" size:15];
          //accountNumber.placeholder = @"MLGW Account Number";
          accountNumber.autocorrectionType = UITextAutocorrectionTypeNo;
          [accountNumber setClearButtonMode:UITextFieldViewModeWhileEditing];
          accountNumber.keyboardType = UIKeyboardTypeNumberPad;
          accountNumber.returnKeyType = UIReturnKeyNext;
          accountNumber.textColor  = [UIColor lightGrayColor];
          [accountNumber setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
          [accountNumber setTag:2];
          [accountNumber setText:@""];
          accountNumber.background = [UIImage imageNamed:@"textfieldBG.png"];
  
          [cell.contentView  addSubview:accountNumber];
  
      }
      if ((indexPath.row == 2)  &amp;&amp; (indexPath.section == 0)){
          address = [[UITextField alloc] initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 200, 25)];
          address.font = [UIFont fontWithName:@"HelveticaNeue" size:15];
          address.placeholder = @"Address";
          address.autocorrectionType = UITextAutocorrectionTypeNo;
          [address setClearButtonMode:UITextFieldViewModeWhileEditing];
          address.keyboardType = UIKeyboardTypeDefault;
          address.returnKeyType = UIReturnKeyNext;
          address.textColor  = [UIColor lightGrayColor];
          [address setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
          [address setTag:3];
          [address setText:@""];
          address.background = [UIImage imageNamed:@"textfieldBG.png"];
          [cell.contentView  addSubview:address];
  
      }
      if ((indexPath.row == 3)  &amp;&amp; (indexPath.section == 0)){
          aptNumber = [[UITextField alloc] initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 200, 25)];
          aptNumber.font = [UIFont fontWithName:@"HelveticaNeue" size:15];
          aptNumber.placeholder = @"Apartment Number";
          aptNumber.autocorrectionType = UITextAutocorrectionTypeNo;
          aptNumber.returnKeyType = UIReturnKeyNext;
          [aptNumber setClearButtonMode:UITextFieldViewModeWhileEditing];
          aptNumber.keyboardType = UIKeyboardTypeNamePhonePad;
          aptNumber.textColor  = [UIColor lightGrayColor];
          [aptNumber setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
          [aptNumber setTag:4];
          [aptNumber setText:@""];
          aptNumber.background = [UIImage imageNamed:@"textfieldBG.png"];
          [cell.contentView  addSubview:aptNumber];
  
      }
      if ((indexPath.row == 4)  &amp;&amp; (indexPath.section == 0)){
          city = [[UITextField alloc] initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 180, 25)];
          city.font = [UIFont fontWithName:@"HelveticaNeue" size:15];
          city.placeholder = @"City";
          city.autocorrectionType = UITextAutocorrectionTypeNo;
          city.returnKeyType = UIReturnKeyNext;
          [city setClearButtonMode:UITextFieldViewModeWhileEditing];
          city.keyboardType = UIKeyboardTypeDefault;
          city.textColor  = [UIColor lightGrayColor];
          [city setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
          [city setTag:5];
          [city setText:@""];
          city.background = [UIImage imageNamed:@"textfieldBG.png"];
          [cell.contentView  addSubview:city];
  
          zip = [[UITextField alloc] initWithFrame:CGRectMake(cell.frame.origin.x+210, cell.frame.origin.y+10,80, 25)];
          zip.font = [UIFont fontWithName:@"HelveticaNeue" size:15];
          zip.placeholder = @"Zip";
          zip.autocorrectionType = UITextAutocorrectionTypeNo;
          zip.returnKeyType = UIReturnKeyNext;
          [zip setClearButtonMode:UITextFieldViewModeWhileEditing];
          zip.keyboardType = UIKeyboardTypeNumberPad;
          zip.textColor  = [UIColor lightGrayColor];
          [zip setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
          [zip setTag:6];
          [zip setText:@""];
          zip.background = [UIImage imageNamed:@"textfieldBG.png"];
          [cell.contentView  addSubview:zip];
  
      }
       if ((indexPath.row == 5)  &amp;&amp; (indexPath.section == 0)){
          phoneNbr = [[UITextField alloc] initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 200, 25)];
          phoneNbr.font = [UIFont fontWithName:@"HelveticaNeue" size:15];
          phoneNbr.placeholder = @"Phone Number";
          phoneNbr.autocorrectionType = UITextAutocorrectionTypeNo;
          phoneNbr.returnKeyType = UIReturnKeyNext;
          [phoneNbr setClearButtonMode:UITextFieldViewModeWhileEditing];
          phoneNbr.keyboardType = UIKeyboardTypeNumberPad;
          phoneNbr.textColor  = [UIColor lightGrayColor];
          [phoneNbr setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
          [phoneNbr setTag:7];
          [phoneNbr setText:@""];
          phoneNbr.background = [UIImage imageNamed:@"textfieldBG.png"];
          [cell.contentView  addSubview:phoneNbr];
  
      }
      if ((indexPath.row == 6)  &amp;&amp; (indexPath.section == 0)){
          emailAddress = [[UITextField alloc] initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 200, 25)];
          emailAddress.font = [UIFont fontWithName:@"HelveticaNeue" size:15];
          emailAddress.placeholder = @"Email Address";
          emailAddress.returnKeyType = UIReturnKeyNext;
          emailAddress.autocorrectionType = UITextAutocorrectionTypeNo;
          [emailAddress setClearButtonMode:UITextFieldViewModeWhileEditing];
          emailAddress.keyboardType = UIKeyboardTypeEmailAddress;
          emailAddress.textColor  = [UIColor lightGrayColor];
          [emailAddress setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
          [emailAddress setTag:8];
          [emailAddress setText:@""];
          emailAddress.background = [UIImage imageNamed:@"textfieldBG.png"];
          [cell.contentView  addSubview:emailAddress];
  
      }
      if ((indexPath.row == 7)  &amp;&amp; (indexPath.section == 0)){
          validateEmail = [[UITextField alloc] initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 200, 25)];
          validateEmail.font = [UIFont fontWithName:@"HelveticaNeue" size:15];
          validateEmail.placeholder = @"Re-enter Email Address";
          validateEmail.returnKeyType = UIReturnKeyNext;
          validateEmail.autocorrectionType = UITextAutocorrectionTypeNo;
          [validateEmail setClearButtonMode:UITextFieldViewModeWhileEditing];
          validateEmail.keyboardType = UIKeyboardTypeEmailAddress;
          validateEmail.textColor  = [UIColor lightGrayColor];
          [validateEmail setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
          [validateEmail setTag:9];
          [validateEmail setText:@""];
          validateEmail.background = [UIImage imageNamed:@"textfieldBG.png"];
          [cell.contentView  addSubview:validateEmail];
  
      }
      if ((indexPath.row == 0)  &amp;&amp; (indexPath.section == 1)){
           oneTimeDonationBtn = [[UIButton alloc]initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 38, 32)];
          [oneTimeDonationBtn setImage:[UIImage imageNamed:@"radioBox.png"] forState:UIControlStateNormal];
          [oneTimeDonationBtn addTarget:self action:@selector(oneTimeAmountAction:) forControlEvents:UIControlEventTouchUpInside];
          oneTimeDonation = [[UITextField alloc] initWithFrame:CGRectMake(oneTimeDonationBtn.frame.origin.x+35, cell.frame.origin.y+13, 150, 25)];
          oneTimeDonation.font = [UIFont fontWithName:@"HelveticaNeue" size:15];
          oneTimeDonation.placeholder = @"One Time Donation";
          oneTimeDonation.autocorrectionType = UITextAutocorrectionTypeNo;
          oneTimeDonation.returnKeyType = UIReturnKeyNext;
          [oneTimeDonation setClearButtonMode:UITextFieldViewModeWhileEditing];
          oneTimeDonation.keyboardType = UIKeyboardTypeDecimalPad;
          oneTimeDonation.textColor  = [UIColor lightGrayColor];
          [oneTimeDonation setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
          [oneTimeDonation setTag:10];
          [oneTimeDonation setText:@""];
          oneTimeDonation.background = [UIImage imageNamed:@"textfieldBG.png"];
          [cell.contentView  addSubview:oneTimeDonationBtn];
          [cell.contentView  addSubview:oneTimeDonation];
  
      }
      if ((indexPath.row == 0) &amp;&amp; (indexPath.section == 2)){
          oneDollarBtn = [[UIButton alloc]initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 38, 32)];
           [oneDollarBtn setImage:[UIImage imageNamed:@"radioBox.png"] forState:UIControlStateNormal];
          [oneDollarBtn addTarget:self action:@selector(oneDollarAction:) forControlEvents:UIControlEventTouchUpInside];
          [cell.contentView  addSubview:oneDollarBtn];
  
          oneDollarLbl = [[UILabel alloc ] initWithFrame:CGRectMake(oneDollarBtn.frame.origin.x+35, cell.frame.origin.y+10, 38, 32)];
          oneDollarLbl.text = @"$1";
          oneDollarLbl.textColor = [UIColor lightGrayColor];
          [cell.contentView  addSubview:oneDollarLbl];
  
          fiveDollarBtn = [[UIButton alloc]initWithFrame:CGRectMake(oneDollarLbl.frame.origin.x+35, cell.frame.origin.y+10, 38, 32)];
          [fiveDollarBtn setImage:[UIImage imageNamed:@"radioBox.png"] forState:UIControlStateNormal];
          [fiveDollarBtn addTarget:self action:@selector(fiveDollarAction:) forControlEvents:UIControlEventTouchUpInside];
          [cell.contentView  addSubview:fiveDollarBtn];
  
          fiveDollarLbl = [[UILabel alloc ] initWithFrame:CGRectMake(fiveDollarBtn.frame.origin.x+35, cell.frame.origin.y+10, 38, 32)];
          fiveDollarLbl.text = @"$5";
          fiveDollarLbl.textColor = [UIColor lightGrayColor];
          [cell.contentView  addSubview:fiveDollarLbl];
  
          tenDollarBtn = [[UIButton alloc]initWithFrame:CGRectMake(fiveDollarLbl.frame.origin.x+35, cell.frame.origin.y+10, 38, 32)];
          [tenDollarBtn setImage:[UIImage imageNamed:@"radioBox.png"] forState:UIControlStateNormal];
          [tenDollarBtn addTarget:self action:@selector(tenDollarAction:) forControlEvents:UIControlEventTouchUpInside];
          [cell.contentView  addSubview:tenDollarBtn];
  
          tenDollarLbl = [[UILabel alloc ] initWithFrame:CGRectMake(tenDollarBtn.frame.origin.x+35, cell.frame.origin.y+10, 38, 32)];
          tenDollarLbl.text = @"$10";
          tenDollarLbl.textColor = [UIColor lightGrayColor];
          [cell.contentView  addSubview:tenDollarLbl];
      }
  
  if ((indexPath.row == 1)  &amp;&amp; (indexPath.section == 2)){
      otherAmountBtn = [[UIButton alloc]initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 38, 32)];
      [otherAmountBtn setImage:[UIImage imageNamed:@"radioBox.png"] forState:UIControlStateNormal];
      [otherAmountBtn addTarget:self action:@selector(otherAmountAmountAction:) forControlEvents:UIControlEventTouchUpInside];
          otherAmount = [[UITextField alloc] initWithFrame:CGRectMake(otherAmountBtn.frame.origin.x+35, cell.frame.origin.y+13, 150, 25)];
          otherAmount.font = [UIFont fontWithName:@"HelveticaNeue" size:15];
          otherAmount.placeholder = @"Other Amount";
          otherAmount.autocorrectionType = UITextAutocorrectionTypeNo;
          otherAmount.returnKeyType = UIReturnKeyNext;
          [otherAmount setClearButtonMode:UITextFieldViewModeWhileEditing];
          otherAmount.keyboardType = UIKeyboardTypeDecimalPad;
          otherAmount.textColor  = [UIColor lightGrayColor];
          [otherAmount setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
          [otherAmount setTag:10];
          [otherAmount setText:@""];
          otherAmount.background = [UIImage imageNamed:@"textfieldBG.png"];
      [cell.contentView  addSubview:otherAmountBtn];
          [cell.contentView  addSubview:otherAmount];
  
      }
  
  
      if ((indexPath.row == 0)  &amp;&amp; (indexPath.section == 3)){
          comments = [[UITextView alloc] initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 280, 100)];
          comments.font = [UIFont fontWithName:@"HelveticaNeue" size:15];
          comments.autocorrectionType = UITextAutocorrectionTypeNo;
          comments.keyboardType = UIKeyboardTypeDefault;
          comments.textColor  = [UIColor lightGrayColor];
          [comments setTag:11];
          [comments setText:@""];
          comments.contentInset = UIEdgeInsetsMake(2.0, 1.0, 0.0, 0.0);
          [cell.contentView  addSubview:comments];
  
  
      }
  }
  
  
  //cell.imageView.image = [UIImage imageNamed:@"settings_icon.png"];
  cell.selectionStyle = UITableViewCellSelectionStyleNone;
  cell.backgroundColor = [UIColor clearColor];
  
  return cell;
  }
  </code></pre>