2010-10-28 8 views
2

Je me suis cogné la tête contre le mur sur celui-ci depuis un certain temps maintenant. Toute contribution ou direction est grandement appréciée.UITextField dans la cellule UITableView renvoie null

L'objectif est donc de créer un formulaire de connexion à partir des champs de texte d'une table. Ces informations d'utilisateur, une fois collectées, seront transmises à un tableau dans un contrôleur de vue séparé afin de pouvoir être stockées dans une liste de "favoris".

J'ai donc créé le formulaire qui a l'air bien et tout sauf quand je console les résultats de formulaire les champs sont retournés (null). J'ai choisi le site pour les réponses, mais rien ne peut être exact. Ci-dessous, la méthode cellForRowAtIndexPath: Je pense que le problème est peut-être lié à la création de textFields. Encore une fois, toute contribution est appréciée!

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
     static NSString *CellIdentifier = @"Cell"; 
     NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]]; 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

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

       textField = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)]; 
       [textField retain]; 
     } 

     textField.adjustsFontSizeToFitWidth = NO; 
     textField.font = [UIFont fontWithName:@"Helvetica" size:14.0]; 
     textField.textColor = [UIColor darkGrayColor]; 
     textField.returnKeyType = UIReturnKeyDone; 
     textField.backgroundColor = [UIColor clearColor]; 
     textField.autocorrectionType = UITextAutocorrectionTypeNo; 
     textField.autocapitalizationType = UITextAutocapitalizationTypeNone; 
     textField.textAlignment = UITextAlignmentLeft; 
     textField.clearButtonMode = UITextFieldViewModeNever; 
     textField.delegate = self; 

     if ([indexPath section] == 0) { 
       switch (indexPath.row) { 
         case 0: 
           textField.placeholder = @"Optional"; 
           textField.secureTextEntry = NO; 
           textField.keyboardType = UIKeyboardTypeDefault; 
           textField.tag = 0; 
           break; 
         case 1: 
           textField.placeholder = @""; 
           textField.secureTextEntry = NO; 
           textField.keyboardType = UIKeyboardTypeDefault; 
           textField.tag = 1; 
           break; 
         case 2: 
           textField.placeholder = @""; 
           textField.secureTextEntry = NO; 
           textField.keyboardType = UIKeyboardTypeDefault; 
           textField.tag = 2; 
           break; 
         case 3: 
           textField.placeholder = @""; 
           textField.secureTextEntry = YES; 
           textField.keyboardType = UIKeyboardTypeDefault; 
           textField.tag = 3; 
           break; 
         case 4: 
           textField.placeholder = @"Optional"; 
           textField.secureTextEntry = NO; 
           textField.keyboardType = UIKeyboardTypeDefault; 
           textField.tag = 4; 
           break; 
         case 5: 
           textField.placeholder = @"Optional"; 
           textField.secureTextEntry = NO; 
           textField.keyboardType = UIKeyboardTypeNumberPad; 
           textField.tag = 5; 
           break; 
       }    
     } 

     [textField setEnabled: YES]; 

     [cell addSubview:textField]; 

    cell.textLabel.text = [listData objectAtIndex:indexPath.row]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     cell.editing = YES; 

     return cell; 
} 

Répondre

2

Créez une cellule personnalisée pour placer votre champ de texte, pour l'amour de Dieu. Vous ne devriez pas avoir le code correspondant addSubview: dans votre tableView:cellForRowAtIndexPath:; juste le code qui alloue la cellule, et configure la cellule assez pour que la cellule elle-même puisse afficher les choses comme vous le voulez.

Regardez la suite de vue de table pour un exemple d'utilisation de cellules personnalisées. Je crois que 4_ et 5_ ont des cellules personnalisées.

+1

Point pris. Alors que j'ai complètement l'intention de revenir en arrière et de créer une cellule personnalisée, est-ce que cela répond à ma question initiale? –

+0

Ce sera, quand vous allez-y et le faire. – jer

7

La variable textField (je suppose que c'est un ivar dans la classe ou une variable globale statique) est votre problème principal. Vous créez un nouveau champ de texte chaque fois que vous créez une nouvelle cellule, ce qui est correct, mais vous l'ajoutez ensuite à une cellule chaque fois que la méthode cellForRowAtIndexPath est appelée. Puisque les cellules sont réutilisées, cela va gâcher les choses.

Votre code doivent ressembler à ceci:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 
    NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]]; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

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

      cell.selectionStyle = UITableViewCellSelectionStyleNone; 
      cell.accessoryType = UITableViewCellAccessoryNone; 
      cell.editing = YES; 

      UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)]; 
      textfield.tag = 1; 
      textField.adjustsFontSizeToFitWidth = NO; 
      textField.font = [UIFont fontWithName:@"Helvetica" size:14.0]; 
      textField.textColor = [UIColor darkGrayColor]; 
      textField.returnKeyType = UIReturnKeyDone; 
      textField.backgroundColor = [UIColor clearColor]; 
      textField.autocorrectionType = UITextAutocorrectionTypeNo; 
      textField.autocapitalizationType = UITextAutocapitalizationTypeNone; 
      textField.textAlignment = UITextAlignmentLeft; 
      textField.clearButtonMode = UITextFieldViewModeNever; 
      textField.delegate = self; 
      [textField setEnabled: YES]; 

      [cell.contentView addSubview:textField]; 
      [textField release]; 
    } 

    UITextField *textField = (UITextField *) [cell.contentView viewWithTag:1]; 

    if ([indexPath section] == 0) { 
      switch (indexPath.row) { 
        case 0: 
          textField.placeholder = @"Optional"; 
          textField.secureTextEntry = NO; 
          textField.keyboardType = UIKeyboardTypeDefault; 
          break; 
        case 1: 
          textField.placeholder = @""; 
          textField.secureTextEntry = NO; 
          textField.keyboardType = UIKeyboardTypeDefault; 
          break; 
        case 2: 
          textField.placeholder = @""; 
          textField.secureTextEntry = NO; 
          textField.keyboardType = UIKeyboardTypeDefault; 
          break; 
        case 3: 
          textField.placeholder = @""; 
          textField.secureTextEntry = YES; 
          textField.keyboardType = UIKeyboardTypeDefault; 
          break; 
        case 4: 
          textField.placeholder = @"Optional"; 
          textField.secureTextEntry = NO; 
          textField.keyboardType = UIKeyboardTypeDefault; 
          break; 
        case 5: 
          textField.placeholder = @"Optional"; 
          textField.secureTextEntry = NO; 
          textField.keyboardType = UIKeyboardTypeNumberPad; 
          break; 
      }    
    } 

    textField.text = [listData objectAtIndex:indexPath.row]; 

    return cell; 
} 

Je ne sais pas ce fait exactement ce que vous voulez, mais il devrait vous orienter dans la bonne direction.

+1

Et comme souligné par jer, lorsque vous avez autant de code d'installation pour une cellule, c'est une bonne idée de le déplacer vers une sous-classe personnalisée UITableViewCell. – loomer

+0

Rétrospective qui a beaucoup de sens, merci. –