UITableViewCell 内的 UITextField

发布于 2024-09-25 04:28:48 字数 3925 浏览 4 评论 0原文

我目前在两个相应的 UITableViewCells 中有两个 UITextFields。

它看起来是这样的:

- (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 出了什么问题以及为什么它不断返回(null)吗?

感谢所有帮助!

I currently have two UITextFields inside two corresponding UITableViewCells.

This is how it looks:

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

As you can see, these work and when I load the UITableView the UITextFields load into the correct UITableViewCells. However, as you can see I pull two NSUserDefault objects, both placed into the corresponding UITextFields.

However, I then have another action attached to a save button which is displayed in the Navigation Bar.

-(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.



}
}

However, when I call these NSLogs, tUser returns (null) but tPass returns the entered text. I am sure it is just a simple syntax error or something of the sort, as everything (from my point of view) looks like it should work. Albeit, it doesn't.

Can someone aid me in figuring out what is wrong with the tUser UITextField and as to why it keeps returning (null)?

All help is appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽容 2024-10-02 04:28:48

每次显示单元格时,您都会添加新的 tUsertPass 文本字段。您应该将单元格创建代码(添加文本字段)保留在 if (cell == nil) 块内,并在该块外部配置这些文本字段。这样,您就不会在每次调用 tableView:cellForRowAtIndexPath: 时添加新的文本字段。

You're adding new tUser and tPass text fields every time your cells get displayed. You should keep your cell creation code (adding of text fields) inside the if (cell == nil) block and configure those text fields outside of that block. That way, you won't add new text fields every time tableView:cellForRowAtIndexPath: is called.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文