【问题标题】:UITextField in UITableView cell is returning nullUITableView 单元格中的 UITextField 返回 null
【发布时间】:2011-05-02 00:47:44
【问题描述】:

很长一段时间以来,我一直在用头撞墙。非常感谢任何输入或指导。

所以目标是从表中的文本字段创建一个登录表单。该用户信息一旦收集,将被传递到单独视图控制器中的数组,以便可以存储在“收藏夹”列表中。

所以我创建了一个看起来很棒的表单,但是当我控制台输出表单结果时,字段返回(null)。我已经选择了该网站的答案,但不能准确。下面是 cellForRowAtIndexPath: 方法,我觉得问题可能出在我创建文本字段的位置。再次感谢您的任何意见!

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

【问题讨论】:

    标签: objective-c ipad uitableview uitextfield


    【解决方案1】:

    textField 变量(我假设它是类中的 ivar 或静态全局变量)是您的主要问题。每次创建新单元格时都会创建一个新的文本字段,这很好,但是每次调用 cellForRowAtIndexPath 方法时都将其添加到单元格中。由于细胞被重复使用,这会搞砸。

    您的代码需要如下所示:

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

    不确定这是否完全符合您的要求,但它应该为您指明正确的方向。

    【讨论】:

    • 正如 jer 所指出的,当您有这么多的单元格设置代码时,最好将其移至自定义 UITableViewCell 子类。
    【解决方案2】:

    为了上帝的爱,创建一个自定义单元格来放置您的文本字段。你的tableView:cellForRowAtIndexPath: 中不应该有addSubview: 相关代码;只需分配单元格的代码,并充分配置单元格,以便单元格本身可以按照您想要的方式显示内容。

    查看表格视图套件,了解如何使用自定义单元格的示例。我相信 4_ 和 5_ 有自定义单元格。

    【讨论】:

    • 点了。虽然我现在完全打算返回并创建一个自定义单元格,但这是否回答了我最初的问题?
    • 它会的,当你继续做的时候。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多