Ios 多个单元格,但仍有重复的tableview内容

Ios 多个单元格,但仍有重复的tableview内容,ios,objective-c,uitableview,Ios,Objective C,Uitableview,有人建议我为每个部分创建多个子类,因为我正在创建一个包含许多不同内容的tableview,问题是无论我做什么,它都会随机复制每个单元格的内容。我做错了什么 viewDidLoad cellForRowAtIndexPath 您将看到重复的内容,因为您将自定义contentView的子视图作为UITableViewDataSource的属性。例如,对节0中的每个单元格使用self.cellName。假设第0节中有5个cellName类型的单元格。如果您希望在所有5个单元格上都有一个自定义UILab

有人建议我为每个部分创建多个子类,因为我正在创建一个包含许多不同内容的tableview,问题是无论我做什么,它都会随机复制每个单元格的内容。我做错了什么

viewDidLoad cellForRowAtIndexPath
您将看到重复的内容,因为您将自定义contentView的子视图作为UITableViewDataSource的属性。例如,对节0中的每个单元格使用self.cellName。假设第0节中有5个cellName类型的单元格。如果您希望在所有5个单元格上都有一个自定义UILabel,那么您需要为每个单元格分配/init 5个单独的UILabel,关键是每个标签需要5个单独的指针。现在您正在使用一个self.namelab。有很多方法可以解决这个问题。一种方法是将自定义子视图作为两个UITableViewCell子类的属性

identifier0 = @"Cell0";
identifier1 = @"Cell1";
identifier2  = @"Cell2";

[self.tableView registerClass:[accountTableViewCell class] forCellReuseIdentifier:identifier0];
[self.tableView registerClass:[NotificationTableViewCell class] forCellReuseIdentifier:identifier1];
- (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;







}