【问题标题】:UiTextField in a UITableViewCell get empty when scrolling滚动时 UITableViewCell 中的 UiTextField 变空
【发布时间】:2011-08-03 20:16:34
【问题描述】:

我正在使用分组的 UITableView 来显示我的应用程序首选项。
我得到了这个单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSInteger section = [indexPath section];
    NSInteger row = [indexPath row];

    NSInteger identifier = row + ((section + 1) * 10);

    NSString *CellIdentifier = [NSString stringWithFormat:@"CustomCellIdentifier-%d",identifier];

    LoginTableViewCell *cell = (LoginTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)    {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"LoginTableViewCell" owner:self options:nil];
        cell = [topLevelObjects objectAtIndex:0];

        cell.field.tag = identifier;

        if (section == 0) {

            switch (row) {
                case 0:{
                    NSString *user = [[NSUserDefaults standardUserDefaults] valueForKey:@"username"];
                    cell.field.text = user;
                    cell.label.text = @"User";
                    break;
                }
                case 1:{
                    NSString *bundleName = [[NSBundle mainBundle] bundleIdentifier];
                    NSError *error = nil;
                    NSString *user = [[NSUserDefaults standardUserDefaults] objectForKey:@"username"];
                    NSString *pwd = [SFHFKeychainUtils getPasswordForUsername:user andServiceName:bundleName error:&error];
                    cell.field.text = pwd;
                    cell.field.secureTextEntry = YES;
                    cell.label.text = @"Password";
                    break;
                }
                case 2:{
                    NSString *numero = [[NSUserDefaults standardUserDefaults] valueForKey:@"numero"];
                    cell.field.text = numero;
                    cell.field.keyboardType = UIKeyboardTypePhonePad;
                    cell.label.text = @"Number";
                    break;
                }
                default:
                    break;
            }

            if (cell.field.text > 0) {
                cell.field.enabled = NO;
                cell.field.borderStyle = UITextBorderStyleNone;
            }

        } else {
            switch (row) {
                case 0:{
                    cell.field.text = [NSString stringWithFormat:@"+%@", [[NSUserDefaults standardUserDefaults] valueForKey:@"prefix"]];
                    cell.field.keyboardType = UIKeyboardTypePhonePad;
                    cell.label.text = @"Prefix";
                    break;
                }

                case 1:{
                    cell.field.text = [[NSUserDefaults standardUserDefaults] valueForKey:@"sender"];
                    cell.field.keyboardType = UIKeyboardTypeNamePhonePad;
                    cell.label.text = @"Sender";
                    break;
                }
                default:
                    break;
            }

        }

    }

    return cell;
}

问题是,当一行滚动到窗口外时,文本字段为空。 如何防止这种情况发生?
我认为缓存是正确的解决方案,但即使使用可重用标识符,我仍然遇到同样的问题。

【问题讨论】:

    标签: iphone uitableview scroll uitextfield editing


    【解决方案1】:

    您正在使用的方法,即从 nib 文件加载表格单元格,需要 - 为了获得正确的可重用单元格支持 - 在 nib“标识符”字段中分配您在代码中使用的相同字符串标识符.也就是说,如果用于缓存双端队列的单元格标识符是“MyCellId”,那么 nib 文件中的 UITableCellView“Identifier”字段必须包含相同的“MyCellId”标识符。 但是使用您遵循的方法这是不可能的,因为单元 ID 是动态生成的,然后您当然不能将单元标识符动态分配给 nib 加载的单元。 理论上,您必须为您生成的每个 MyCellId-%d 标识符创建一个特定的 nib 文件。 我假设您只定义了一个表格单元格,摆脱动态标识符生成并使用静态名称,然后将此名称分配给单元格 nib 文件中的“标识符”字段。

    【讨论】:

    • 你是对的。似乎问题与 nib 文件标识符有关。这就是我讨厌IB的原因。我将以编程方式进行旧式操作。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多