开发者

UITextField in UITableView cell is returning as (null)

The two text fields are in the UItableView. The build is getting completed without any error. When I enter login details and hit submit button at UINavController, the first field is returning as (null). I'm not able to find the reason why that's happening.

Here's the code that I'm using:

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

    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 row] == 0) {
                tUser.placeholder = @"user@domain.com";
                tUser.keyboardType = UIKeyboardTypeEmailAddress;
                tUser.returnKeyType = UIReturnKeyNext;
            }
            if ([indexPath row] == 1) {
                tPass.placeholder = @"Required";
                tPass.keyboardType = UIKeyboardTypeDefault;
                tPass.returnKeyType = UIReturnKeyDone;
                [tPass addTarget:self
                                      action:@selector(save:)
                forControlEvents:UIControlEventEditingDidEndOnExit];

                tPass.secureTextEntry = YES;
            }
        }

        tUser.autocorrectionType = UITextAutocorrectionTypeNo;
        tUser.autocapitalizationType = UITextAutocapitalizationTypeNone;
        tUser.textAlignment = UITextAlignmentLeft;

        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];

     // Email & Password Section
        if ([indexPath row] == 0) { // Email
            cell.textLabel.text = @"Username";
            [cell addSubview:tUser];
开发者_开发技巧        }
        else {
            cell.textLabel.text = @"Password";
            [cell addSubview:tPass];
        }

    return cell;    
}

-(IBAction) save: (id) sender {

        if ([tPass text] == nil) {
            UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"Error" 
                                                            message:@"There was no password entered, please enter the correct password and try again." 
                                                           delegate:self 
                                                  cancelButtonTitle:@"Okay" 
                                                  otherButtonTitles:nil];
            [alertV show];
            [alertV 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]);


        }

    }


One issue is that you keep re-creating the text fields whenever the table needs to re-create a cell, which could happen multiple times for single cell. You do not show any indication how you are later accessing these text fields in order to retrieve the values, such as using the control's tag property.

When you dequeue a cell, that cell will have its subviews intact, so you should use the tag property to retrieve the text field that is already there and update the text on it instead of creating a new text field every time.

Also, a better approach would be to create a custom UITableViewCell subclass that includes the text field with properties that allow you to easily access the embedded text fields. Then, in your tableView:didSelectRowWithIndexPath: method you can access the particular cell of the selected row. To get the cell, you send a cellForRowAtIndexPath: message to the tableView instance. Once you have the cell, you can access the custom properties to retrieve the username and password.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜