0

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

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

-(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];
}
4

1 回答 1

0

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

static NSString *CellIdentifier = @"anmeldenCell";

或为他们准备重置功能。

您的问题是单元格在部分之间重复使用。您在此处添加子视图 [cell.contentView addSubview:gender]; 而且您永远不会删除它-因此它将多次添加到同一个单元格中+具有此视图的单元格也将在其他部分中使用。

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

顺便提一句。对节使用枚举以增加可读性,而不是 1、2、3、4 使用 kSectionMyProfile 等...

于 2013-10-01T14:03:42.293 回答