2010-09-30 15 views
0

J'ai actuellement deux UITextFields dans deux UITableViewCells correspondants.UITextField à l'intérieur d'un UITableViewCell

Voici à quoi il ressemble:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Cell"; 

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

// Configure the cell. 

//adding all the UITextField's to the UITableViewCell is a pain in the ass. Pretty sure this is correct though. 

if ([indexPath section] == 0) { 
    tUser = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)]; 
    tUser.adjustsFontSizeToFitWidth = YES; 
    tUser.textColor = [UIColor blackColor]; 

    tPass = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)]; 
    tPass.adjustsFontSizeToFitWidth = YES; 
    tPass.textColor = [UIColor blackColor]; 

    if ([indexPath section] == 0) { 
     if ([indexPath row] == 0) { 
      tUser.placeholder = @"@JohnAppleseed"; 
      tUser.keyboardType = UIKeyboardTypeEmailAddress; 
      tUser.returnKeyType = UIReturnKeyNext; 
     } 
     if ([indexPath row] == 1) { 
      tPass.placeholder = @"Required"; 
      tPass.keyboardType = UIKeyboardTypeDefault; 
      tPass.returnKeyType = UIReturnKeyDone; 
      tPass.secureTextEntry = YES; 
     } 
    } 

    tUser.backgroundColor = [UIColor whiteColor]; 
    tUser.autocorrectionType = UITextAutocorrectionTypeNo; 
    tUser.autocapitalizationType = UITextAutocapitalizationTypeNone; 
    tUser.textAlignment = UITextAlignmentLeft; 

    tPass.backgroundColor = [UIColor whiteColor]; 
    tPass.autocorrectionType = UITextAutocorrectionTypeNo; 
    tPass.autocapitalizationType = UITextAutocapitalizationTypeNone; 
    tPass.textAlignment = UITextAlignmentLeft; 

    tUser.clearButtonMode = UITextFieldViewModeNever; 
    tPass.clearButtonMode = UITextFieldViewModeNever; 

    [tUser setEnabled:YES]; 
    [tPass setEnabled:YES]; 

    //[tUser release]; 
    //[tPass release]; 
} 
if ([indexPath section] == 0) { // Email & Password Section 
    if ([indexPath row] == 0) { // Email 
     cell.textLabel.text = @"Username"; 
     [cell addSubview:tUser]; 
     [tUser setText:[[NSUserDefaults standardUserDefaults] objectForKey:@"twitter_name_preference"]]; 
    } 
    else { 
     cell.textLabel.text = @"Password"; 
     [cell addSubview:tPass]; 
     [tPass setText:[[NSUserDefaults standardUserDefaults] objectForKey:@"twitter_pass_preference"]]; 
    } 
} 
return cell; } 

Comme vous pouvez le voir, ce travail et quand je charge le UITableView la charge UITextFields dans les UITableViewCells corrects. Cependant, comme vous pouvez le voir, je tire deux objets NSUserDefault, placés dans les UITextFields correspondants.

Cependant, j'ai alors une autre action attachée à un bouton de sauvegarde qui est affiché dans la barre de navigation. Cependant, lorsque j'appelle ces NSLogs, tUser renvoie (null) mais tPass renvoie le texte entré. Je suis sûr que c'est juste une simple erreur de syntaxe ou quelque chose de ce genre, car tout (de mon point de vue) semble fonctionner. Bien que ce ne soit pas le cas. Est-ce que quelqu'un peut m'aider à trouver ce qui ne va pas avec le tUser UITextField et pourquoi il continue de retourner (null)?

Toute aide est appréciée!

+0

Rien n'est manifestement faux dans ce code que je peux voir. Une chose que vous pouvez vérifier est de voir si 'tUser' est nul au moment où vous frappez ces NSLog. Y a-t-il autre chose que les deux méthodes présentées ici qui changent tUser? –

+0

Rien d'autre ne change du tout. Au point de ces NSLogs, tUser est nul. Cependant, j'ai physiquement entré le texte dans tUser et il tire encore null. –

Répondre

0

Vous ajoutez de nouveaux champs de texte tUser et tPass chaque fois que vos cellules s'affichent. Vous devriez garder votre code de création de cellule (en ajoutant des champs de texte) à l'intérieur du bloc if (cell == nil) et configurer les champs de texte à l'extérieur de ce bloc. De cette façon, vous n'ajouterez pas de nouveaux champs de texte à chaque fois que tableView:cellForRowAtIndexPath: est appelée.

+0

Vous pouvez élaborer en ajoutant un extrait de code? –

+0

Ne vous inquiétez pas, ça marche! :RÉ –