views:

66

answers:

2

Hi all,

I've been banging my head against the wall on this one for quite some time now. Any input or direction is greatly appreciated.

So the goal is the create a log in form from text fields in a table. That user info, once collected will be passed on to an array in a seperate view controller so in can stored in a "favourites" list.

So I've created the form which looks great and all but when I console out the form results the fields are return (null). I've picked the site for answers but can't anything exact. Below is the cellForRowAtIndexPath: method I feel that maybe the issue is where I'm creating the textFields. Again, any input is appreciated!

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifier = @"Cell";
        NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]];

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil) {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 
                                                                                                                                            reuseIdentifier:CellIdentifier] autorelease];

                textField = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
                [textField retain];
        }

        textField.adjustsFontSizeToFitWidth = NO;
        textField.font = [UIFont fontWithName:@"Helvetica" size:14.0];
        textField.textColor = [UIColor darkGrayColor];
        textField.returnKeyType = UIReturnKeyDone;
        textField.backgroundColor = [UIColor clearColor];
        textField.autocorrectionType = UITextAutocorrectionTypeNo; 
        textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
        textField.textAlignment = UITextAlignmentLeft;
        textField.clearButtonMode = UITextFieldViewModeNever;
        textField.delegate = self;

        if ([indexPath section] == 0) {
                switch (indexPath.row) {
                        case 0:
                                textField.placeholder = @"Optional";
                                textField.secureTextEntry = NO;
                                textField.keyboardType = UIKeyboardTypeDefault;
                                textField.tag = 0;
                                break;
                        case 1:
                                textField.placeholder = @"";
                                textField.secureTextEntry = NO;
                                textField.keyboardType = UIKeyboardTypeDefault;
                                textField.tag = 1;
                                break;
                        case 2:
                                textField.placeholder = @"";
                                textField.secureTextEntry = NO;
                                textField.keyboardType = UIKeyboardTypeDefault;
                                textField.tag = 2;
                                break;
                        case 3:
                                textField.placeholder = @"";
                                textField.secureTextEntry = YES;
                                textField.keyboardType = UIKeyboardTypeDefault;
                                textField.tag = 3;
                                break;
                        case 4:
                                textField.placeholder = @"Optional";
                                textField.secureTextEntry = NO;
                                textField.keyboardType = UIKeyboardTypeDefault;
                                textField.tag = 4;
                                break;
                        case 5:
                                textField.placeholder = @"Optional";
                                textField.secureTextEntry = NO;
                                textField.keyboardType = UIKeyboardTypeNumberPad;
                                textField.tag = 5;
                                break;
                }               
        }

        [textField setEnabled: YES];

        [cell addSubview:textField];

  cell.textLabel.text = [listData objectAtIndex:indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.editing = YES;

        return cell;
}
+1  A: 

Create a custom cell to place your text field, for the love of god. You shouldn't have addSubview: related code in your tableView:cellForRowAtIndexPath:; just code that allocates the cell, and configures the cell enough so that the cell itself can display things they way you want them.

Look at the table view suite for an example of how to use custom cells. I believe 4_ and 5_ have custom cells.

jer
Point taken. While I completely intend on going back and creating a custom cell now does that answer my original question though?
Shane Da Silva
It will, when you go ahead and do it.
jer
+3  A: 

The textField variable (I assume it is an ivar in the class or a static global variable) is your main problem. You create a new text field each time you create a new cell, which is fine, but then you add it to a cell every time the cellForRowAtIndexPath method is called. Since cells are reused this will screw things up.

Your code need to look something like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2                                                                                                   reuseIdentifier:CellIdentifier] autorelease];

            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.editing = YES;

            UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
            textfield.tag = 1;
            textField.adjustsFontSizeToFitWidth = NO;
            textField.font = [UIFont fontWithName:@"Helvetica" size:14.0];
            textField.textColor = [UIColor darkGrayColor];
            textField.returnKeyType = UIReturnKeyDone;
            textField.backgroundColor = [UIColor clearColor];
            textField.autocorrectionType = UITextAutocorrectionTypeNo; 
            textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
            textField.textAlignment = UITextAlignmentLeft;
            textField.clearButtonMode = UITextFieldViewModeNever;
            textField.delegate = self;
            [textField setEnabled: YES];

            [cell.contentView addSubview:textField];
            [textField release];
    }

    UITextField *textField = (UITextField *) [cell.contentView viewWithTag:1];

    if ([indexPath section] == 0) {
            switch (indexPath.row) {
                    case 0:
                            textField.placeholder = @"Optional";
                            textField.secureTextEntry = NO;
                            textField.keyboardType = UIKeyboardTypeDefault;
                            break;
                    case 1:
                            textField.placeholder = @"";
                            textField.secureTextEntry = NO;
                            textField.keyboardType = UIKeyboardTypeDefault;
                            break;
                    case 2:
                            textField.placeholder = @"";
                            textField.secureTextEntry = NO;
                            textField.keyboardType = UIKeyboardTypeDefault;
                            break;
                    case 3:
                            textField.placeholder = @"";
                            textField.secureTextEntry = YES;
                            textField.keyboardType = UIKeyboardTypeDefault;
                            break;
                    case 4:
                            textField.placeholder = @"Optional";
                            textField.secureTextEntry = NO;
                            textField.keyboardType = UIKeyboardTypeDefault;
                            break;
                    case 5:
                            textField.placeholder = @"Optional";
                            textField.secureTextEntry = NO;
                            textField.keyboardType = UIKeyboardTypeNumberPad;
                            break;
            }               
    }

    textField.text = [listData objectAtIndex:indexPath.row];

    return cell;
}

Not sure this does exactly what you want but it should point you in the right direction.

loomer
And as pointed out by jer, when you have this much setup code for a cell it's a good idea to move that to a custom UITableViewCell subclass.
loomer
Looking back that makes a lot sense, thanks.
Shane Da Silva