多个单元格,仍然是重复的tableview内容

时间:2014-08-02 10:14:16

标签: ios objective-c uitableview

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

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;







}

1 个答案:

答案 0 :(得分:0)

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