Ios 将覆盖表视图中不可见的单元格
我已经通过代码创建了一个表单,以便在应用程序中注册,但如果我上下滚动不可见的单元格,则会被其他单元格的设置覆盖。。。有没有人经历过这样的事情?我很确定我错过了一些配置,但我不知道是哪个 这是我的密码。 代码太多了……对不起。但我真的不知道从哪里开始Ios 将覆盖表视图中不可见的单元格,ios,uitableview,Ios,Uitableview,我已经通过代码创建了一个表单,以便在应用程序中注册,但如果我上下滚动不可见的单元格,则会被其他单元格的设置覆盖。。。有没有人经历过这样的事情?我很确定我错过了一些配置,但我不知道是哪个 这是我的密码。 代码太多了……对不起。但我真的不知道从哪里开始 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (section == 0) { ret
-(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];
}
对单元格使用不同的标识符:
static NSString *CellIdentifier = @"anmeldenCell";
或者为它们准备重置功能
您的问题是单元在分区之间重复使用。您正在此处添加子视图[cell.contentView addSubview:gender];而且您永远不会删除它-因此它将被添加到同一单元格中多次+具有此视图的单元格也将在其他部分中使用
最好的方法是为每个部分准备定制单元,并为它们使用不同的单元标识符。此外,当您重新使用它们时,将一个视图分配给它是正确的,而不是多次添加子视图
顺便说一句,对节使用枚举以提高可读性,而不是使用kSectionMyProfile,等等。但这不应该由dequeReusableCellWithIdentifier来处理吗:?它是由它来处理的-但如果您对所有不同的单元格使用相同的标识符,则将它们视为相同的单元格-并且您不会得到以前使用过的单元格。试想一下:您将子视图添加到一个单元格中—您永远不会删除它。“出列”将在每次通过该函数时再次返回相同的单元格,并向其添加子视图。最简单的解决方案是为每个节/行创建标识符。但如果像我在代码中看到的那样,将它们计算在内,这并不是真正的最优。您还可以查看-(void)prepareforeuse;方法-我建议将自定义视图添加到1个容器中-并删除该函数中的容器-使用该方法,您可以保持代码不变并使用1个标识符。而不仅仅是一个自定义类来处理要实现的TableViewCell,这对您来说可能是一个足够好的解决方案。