2010-09-30 77 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

相关问题