滚动UITableView更改存储在NSUserDefault中的UITextField上的数据

时间:2012-02-13 22:46:11

标签: uitableview scroll uitextfield nsuserdefaults

我为每一行都有一个带TextField的TableView。 TableView由两部分组成。 当用户点击KeyBoard的返回按钮时,我在NSUserDefault上设置TextField的文本。 在cellForRowAtIndexPath方法中,我读取了NSUserDefault上的文本值。 为什么当我滚动TableView时,数据会发生变化?

cellForRowAtIndexPath

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {        
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

//set cell value from NSArray
if ((indexPath.section) == 0) {
    cell.textLabel.text = [sicurezzaSullaStrada objectAtIndex:indexPath.row];
} else {
    cell.textLabel.text = [infoVeicolo objectAtIndex:indexPath.row];
}


//set TextField value from NSUserDefault
if (indexPath.section == 0) {
    //first section
    if (indexPath.row == 0) {
        TextFieldCell = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 80, 21)];
        TextFieldCell.adjustsFontSizeToFitWidth = YES;
        TextFieldCell.placeholder = @"Enter text";
        TextFieldCell.delegate = self;
        [TextFieldCell setText:[[NSUserDefaults standardUserDefaults] objectForKey:@"stringPesoKg"]];
        cell.accessoryView = TextFieldCell;
        }
    if (indexPath.row == 1) {
        TextFieldCell = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 80, 21)];
        TextFieldCell.adjustsFontSizeToFitWidth = YES;
        TextFieldCell.placeholder = @"Enter Text";
        TextFieldCell.delegate = self;
        [TextFieldCell setText:[[NSUserDefaults standardUserDefaults] objectForKey:@"stringPostiLettoOmologati"]];
       cell.accessoryView = TextFieldCell;
    } else {
    //second section
    if (indexPath.row == 0) {
        //Riga relativa alla Data di immatricolazione
        TextFieldCell = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 80, 21)];
        TextFieldCell.adjustsFontSizeToFitWidth = YES;
        TextFieldCell.placeholder = @"Enter Text";
        TextFieldCell.delegate = self;
        [TextFieldCell setText:[[NSUserDefaults standardUserDefaults] objectForKey:@"stringDataImmatricolazione"]];
        cell.accessoryView = TextFieldCell;
    }

然后在textFieldDidEndEditing

if (posizSect == 0) {
    if (posizRow == 0) {
        NSUserDefaults *stringDefaultPesoKg = [NSUserDefaults standardUserDefaults];
        [stringDefaultPesoKg setObject:textField.text forKey:@"stringPesoKg"];  
    }
    if (posizRow == 1) {
        NSUserDefaults *stringDefaultPostiLettoOmologati = [NSUserDefaults standardUserDefaults];
        [stringDefaultPostiLettoOmologati setObject:textField.text forKey:@"stringPostiLettoOmologati"];  
    }
    if (posizRow == 2) {
        NSUserDefaults *stringDefaultPostiLettoOmologati = [NSUserDefaults standardUserDefaults];
        [stringDefaultPostiLettoOmologati setObject:textField.text forKey:@"stringLunghezzaMax"];  
    } else {
    if (posizRow == 0) {
        NSUserDefaults *stringDefaultPesoKg = [NSUserDefaults standardUserDefaults];
        [stringDefaultPesoKg setObject:textField.text forKey:@"stringDataImmatricolazione"];  
    }

didSelectRowAtIndexPath:上,我设置了PosizRow和PosizSect的值:

posizRow = indexPath.row;
posizSect = indexPath.section;

谢谢, 来自意大利的亚历山德罗

1 个答案:

答案 0 :(得分:0)

数据更改的原因是,当您将UITableViewCells从屏幕上滚动时,它们会出现故障。此时数据消失,当您向后滚动时,将使用初始值(而不是更改的值)重新填充单元格。我不知道如何解决它,我现在正在寻找解决方案。当我找到它时,我会将你链接到它。

*编辑*** 好的,这就是你做的:

首先,您需要使ViewController成为UITextFieldDelegate的委托。然后你需要实现:

  • (void)textFieldDidEndEditing:(UITextField *)textField

在我的例子中,用户界面是在代码中动态完成的,所以我有一个NSMutableDictionary,它包含所有字段的名称。所以在我的代码中我执行以下操作:

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    NSArray *fields = [editTexts allKeysForObject:textField];

    if(fields.count > 0)
    {
        [clonedItem setValue:textField.text forKey:[fields objectAtIndex:0]];
    }
}

如果您将文本字段作为属性,则需要另一种方法来更新存储变量。例如,您可以在textField上使用标记来标识哪个字段。