2010-09-30 78 views
0

我目前在兩個對應的UITableViewCells中有兩個UITextFields。UITableViewCell裏面的UITextField

這是它的外觀:

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

正如你所看到的,這些工作,當我的UITableView的UITextFields負荷加載到正確的UITableViewCells。但是,正如你所看到的那樣,我將兩個NSUserDefault對象放到相應的UITextField中。

但是,然後我有另一個操作附加到導航欄中顯示的保存按鈕。

-(void)save_clicked: (id) sender { 

if ([tPass text] == nil) { 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                message:@"There was no password entered, please enter the correct password and try again." 
                delegate:self 
              cancelButtonTitle:@"Okay" 
              otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
} 

else { 
    NSLog(@"we can do something here soon..."); 

    //NSString *tUserString = [[NSString alloc] initWithFormat:@"Hello: %@", tUser.text]; 

    NSLog(@"We saved their username: %@", [tUser text]); 
    NSLog(@"We saved their password: %@", [tPass text]); 

    // here we will start saving the username and password, then obtaining the authentication shit. 



} 
} 

但是,當我調用這些NSLogs時,tUser返回(null),但tPass返回輸入的文本。我確定它只是一個簡單的語法錯誤或類似的東西,因爲所有事情(從我的角度來看)看起來應該起作用。儘管如此,事實並非如此。

有人可以幫助我找出tUser UITextField有什麼問題,以及它爲什麼會一直返回(空)?

所有幫助表示讚賞!

+0

在我看到的這段代碼中,沒有什麼明顯的錯誤。你可能要檢查的一件事是在你點擊NSLog的時候看看'tUser'是否爲空。除了這裏顯示的兩種方法改變tUser之外,還有其他的東西嗎? – 2010-09-30 03:58:00

+0

沒有別的東西在改變用戶。在這些NSLog的時候,tUser是空的。但是,我已經將文字輸入到用戶身上,並且仍然返回null。 – 2010-09-30 04:04:49

回答

0

您每次顯示單元格時都會添加新的tUsertPass文本字段。您應該在if (cell == nil)塊內保留單元格創建代碼(添加文本字段),並在該塊外部配置這些文本字段。這樣,每次調用tableView:cellForRowAtIndexPath:時都不會添加新的文本字段。

+0

您可以通過添加代碼片段來詳細說明嗎? – 2010-09-30 04:23:28

+0

不要擔心它,讓它工作! :d – 2010-09-30 04:30:05

相關問題