2013-10-01 47 views
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]; 
} 

回答

0

使用不同的標識符細胞:

static NSString *CellIdentifier = @"anmeldenCell"; 

或爲他們準備復位功能。

您的問題是單元之間重用單元。你在這裏添加子視圖[cell.contentView addSubview:gender];你永遠不會刪除它 - 所以它會被添加幾次到同一個單元格+這個視圖的單元格也會在其他部分中使用。

最好的方法是爲每個部分準備自定義單元格,併爲它們使用不同的CellIdentifier。此外,當您重複使用它們時,可以正確地查看視圖,而不是多次添加子視圖。

順便說一句。使用枚舉的部分,以提高可讀性,而不是1,2,3,4使用kSectionMyProfile等...

+0

但不應該被dequeReusableCellWithIdentifier處理:? –

+0

它是由它處理的 - 但是如果你對它們使用相同的標識符,你將所有不同的單元格視爲同一個單元格 - 而且你沒有獲得以前使用過的單元格。試想一下:你將細節添加到細胞 - 你永遠不會刪除它。出隊將再次返回給你同一個單元 - 並且你再次向它添加子視圖 - 每次它通過該函數。 –

+0

好吧,我明白了。將不得不檢查如何解決它現在... –

相關問題