UITextField in UITableView cell is returning null

时间秒杀一切 提交于 2019-12-30 11:14:39

问题


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:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/4045658/uitextfield-in-uitableview-cell-is-returning-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!