Q:

multiple cells and still duplicate tableview content

i've been advised to create multiple subclasses for each section, since i'm creating an tableview with lots of different content, the problem is whatever i do it keep duplicating the content of each cell randomly. What am i doing wrong?
viewDidLoad
identifier0 = @"Cell0";
identifier1 = @"Cell1";
identifier2  = @"Cell2";

[self.tableView registerClass:[accountTableViewCell class] forCellReuseIdentifier:identifier0];
[self.tableView registerClass:[NotificationTableViewCell class] forCellReuseIdentifier:identifier1];

cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

//section 0
self.emailTextField.delegate = self;
self.cellName = [[UILabel alloc] initWithFrame:CGRectMake(20, 12, 130, 20)];
self.cellName.textColor = [UIColor colorWithRed:0.137 green:0.145 blue:0.157 alpha:1];
self.cellName.font = [UIFont fontWithName:@"HelveticaNeue" size:14];

self.nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 7, 190, 30)];
self.nameTextField.enabled = NO;

self.emailTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 7, 190, 30)];
[self.emailTextField setReturnKeyType:UIReturnKeyDone];
[self.emailTextField setKeyboardType:UIKeyboardTypeEmailAddress];

self.genderSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(251, 6, 31, 51)];

//section 1
self.acceptanceSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(251, 6, 31, 51)];
self.likeSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(251, 6, 31, 51)];
self.messageSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(251, 6, 31, 51)];

accountTableViewCell *cell0;
NotificationTableViewCell *cell1;

if(indexPath.section == 0)
{

    cell0 = [tableView dequeueReusableCellWithIdentifier:identifier0 forIndexPath:indexPath];
} else if (indexPath.section == 1) {
     cell1 = [tableView dequeueReusableCellWithIdentifier:identifier1 forIndexPath:indexPath];
}

// This part ensure you will initiate each cell with the correct identifier
if (!cell0)
{
    cell0 = [[accountTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier0];
    cell0.backgroundColor = [UIColor colorWithRed:0.953 green:0.953 blue:0.965 alpha:1] ;

} else if (!cell1) {

    cell1 = [[NotificationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier1];
    cell1.backgroundColor = [UIColor colorWithRed:0.953 green:0.953 blue:0.965 alpha:1] ;

}

if (indexPath.section == 0) {

    [cell0.contentView addSubview:self.cellName];

    self.cellName.text = [accountArray objectAtIndex:indexPath.row];

    if (indexPath.row == 0) {

        [cell0.contentView addSubview:self.nameTextField];
    } else if (indexPath.row == 1) {

        [cell0.contentView addSubview:self.emailTextField];

    } else if (indexPath.row == 2) {

        [cell0.contentView addSubview:self.genderSwitch];
    }

} else if(indexPath.section == 1) {

    [cell1.contentView addSubview:self.cellName];

    cell1.selectionStyle = UITableViewCellSelectionStyleNone;
    self.cellName.text = [notArray objectAtIndex:indexPath.row];

    if (indexPath.row == 0) {

        [cell1.contentView addSubview:self.acceptanceSwitch];
    } else if (indexPath.row == 1) {

        [cell1.contentView addSubview:self.messageSwitch];
    } else if (indexPath.row == 2) {

        [cell1.contentView addSubview:self.likeSwitch];
    }

}

return cell0;
return cell1;

}

A:

You are seeing duplicate content because you have the custom contentView's subviews as properties of your UITableViewDataSource.  For example, you use self.cellName for every cell in section 0.  Let us say you have 5 cellName-type cells in section 0.  If you want a custom UILabel on all 5 cells then you would need to alloc/init 5 separate UILabels for each cell and the key part is that you need 5 separate pointers for each label.  Right now you are using one, self.nameLabel.  There are many ways you could go about fixing this.  One way would be making the custom subviews as properties of your two UITableViewCell subclasses.

