表视图中不可见的单元格被覆盖

时间:2013-10-01 14:00:00

标签: ios uitableview

我已经通过代码在应用程序中进行了注册,但如果我向上和向下滚动不可见的单元格会被其他单元格的设置覆盖......有没有人经历过这样的事情?我很确定我错过了一些配置,但我不知道哪个。

这是我的代码。 它的代码很多......对不起。但我真的不知道从哪里开始

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 4;
    } else if (section == 1) {
        return 4;
    } else {
        return 5;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 3;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return @"Perönliche Informationen";
    } else if (section == 1) {
        return @"Anschrift";
    } else  {
        return @"myClassico Profil";
    }

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    if ([indexPath section] == 0) {
        if ([indexPath row] == 3) {
            return 210;
        } else {
            return 44;
        }
    }


    else if ([indexPath section] == 1) {
        if ([indexPath row] == 0) {
            return 80;
        } else if ([indexPath row] == 3) {
            return 210;
        } else {
            return 44;
        }
    }


    else if ([indexPath section] == 2) {
        return 44;
    }


    else {
        return 44;
    }

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"anmeldenCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    if ([indexPath section] == 0) {
        if ([indexPath row] == 0) {
            UISegmentedControl *gender = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Frau", @"Herr", nil]];
            gender.frame = CGRectMake(99, 8, 124, 28);
            gender.selectedSegmentIndex = 0;

            [cell.contentView addSubview:gender];


        } else if ([indexPath row] == 1) {
            UITextField *vorname = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 300, 44)];
            vorname.placeholder = @"Vorname";

            [cell.contentView addSubview:vorname];
        } else if ([indexPath row] == 2) {
            UITextField *nachname = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 300, 44)];
            nachname.placeholder = @"Nachname";

            [cell.contentView addSubview:nachname];
        } else {

            //Code below is backup code in case someone really dislikes the UIDatePicker. Code is fully functional.
            /*
            UITextField *gebTag = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 80, 44)];
            UITextField *gebMonat = [[UITextField alloc] initWithFrame:CGRectMake(108, 0, 100, 44)];
            UITextField *gebJahr = [[UITextField alloc] initWithFrame:CGRectMake(216, 0, 100, 44)];

            gebTag.placeholder = @"TT";
            gebMonat.placeholder = @"MM";
            gebJahr.placeholder = @"JJ";

            [cell.contentView addSubview:gebTag];
            [cell.contentView addSubview:gebMonat];
            [cell.contentView addSubview:gebJahr];*/

            UILabel *gebLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 280, 50)];
            gebLabel.text = @"Geburtsdatum";

            UIDatePicker *gender = [[UIDatePicker alloc] initWithFrame:CGRectMake(20, 30, 320, 180)];


            [cell.contentView addSubview:gender];
            [cell.contentView addSubview:gebLabel];
        } //ENDE SECTION 1 (INFORMATIONEN ZUR PERSON)

    } else if ([indexPath section] == 1) {
        if ([indexPath row] == 0) {
            UITextField *adresseStrasse = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 280, 44)];
            adresseStrasse.placeholder = @"Straße";

            UITextField *adresseHNR = [[UITextField alloc] initWithFrame:CGRectMake(180, 30, 120, 44)];
            adresseHNR.textAlignment = 2;
            adresseHNR.placeholder = @"Hausnummer";

            [cell.contentView addSubview:adresseStrasse];
            [cell.contentView addSubview:adresseHNR];
        } else if ([indexPath row] == 1) {
            UITextField *adresseZusatz = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 280, 44)];
            adresseZusatz.placeholder = @"Adresszusatz (optional)";

            [cell.contentView addSubview:adresseZusatz];
        } else if ([indexPath row] == 2) {
            UITextField *adressePLZ = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 100, 44)];
            adressePLZ.placeholder = @"PLZ";

            UITextField *adresseOrt = [[UITextField alloc] initWithFrame:CGRectMake(110, 0, 170, 44)];
            adresseOrt.placeholder = @"Ort";

            [cell.contentView addSubview:adressePLZ];
            [cell.contentView addSubview:adresseOrt];
        } else  {

            UILabel *land = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 150, 20)];
            land.text = @"Land";

            länderPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(20, 20, 300, 180)];
            [länderPicker setDelegate:self];
            array = [[NSMutableArray alloc] init];
            [array addObject:@"Deutschland"];
            [array addObject:@"Österreich"];
            [array addObject:@"Schweiz"];

            [cell.contentView addSubview:land];
            [cell.contentView addSubview:länderPicker];

        } //ENDE SECTION 2 (ADRESSEN INFORMATIONEN)
    } else if ([indexPath section] == 2){
        if ([indexPath row] == 0) {
            UITextField *email = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 280, 44)];
            email.placeholder = @"E-Mail";

            [cell.contentView addSubview:email];
        } else if ([indexPath row] == 1) {
            UITextField *telefon = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 280, 44)];
            telefon.placeholder = @"Telefon";

            [cell.contentView addSubview:telefon];
        } else if ([indexPath row] == 2) {
            UITextField *mtelefon = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 280, 44)];
            mtelefon.placeholder = @"Mobiltelefon (optional)";

            [cell.contentView addSubview:mtelefon];
        } else if (indexPath.row == 3) {
            UITextField *passwort = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 280, 44)];
            passwort.placeholder = @"Passwort";

            [cell.contentView addSubview:passwort];
        } else {
            UITextField *passwortWiederholung = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 280, 44)];
            passwortWiederholung.placeholder = @"Passwort wiederholen";

            [cell.contentView addSubview:passwortWiederholung];
        } //ENDE SECTION 3 (MYCLASSICO ACCOUNT INFORMATIONEN)
    }


    return cell;
}


-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return [array count];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {

    return 1;
}

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [array objectAtIndex:row];
}

1 个答案:

答案 0 :(得分:0)

为单元格使用不同的标识符:

static NSString *CellIdentifier = @"anmeldenCell";

或为他们准备重置功能。<​​/ p>

您的问题是细胞在各部分之间重复使用。你在这里添加子视图[cell.contentView addSubview:gender];并且你永远不会删除它 - 所以它将被添加到同一个单元格+这个视图的单元格也将被用于其他部分。

最佳方法是为每个部分准备自定义单元格,并为它们使用不同的CellIdentifier。此外,当您重复使用它们时,可以正确地查看它,而不是多次添加子视图。

顺便说一句。使用枚举来增加可读性,而不是1,2,3,4使用kSectionMyProfile等...