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

发布于 2025-01-05 08:41:28 字数 3366 浏览 3 评论 0原文

我有一个 TableView,每行都有 TextField。 TableView 由两部分组成。 当用户点击键盘的返回按钮时,我在 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;

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

I've a TableView with TextField for each row. The TableView is composed by two section.
When the user tap on return button of KeyBoard, I set the text of TextField on NSUserDefault.
In the cellForRowAtIndexPath method i read the value of text on NSUserDefault.
Why when I scroll the TableView, the data changes?

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

And then ontextFieldDidEndEditing:

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"];  
    }

On didSelectRowAtIndexPath:, I set the value of PosizRow and PosizSect:

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

Thanks,
Alessandro from Italy

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ら栖息 2025-01-12 08:41:28

数据更改的原因是,当您将 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]];
    }
}

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

The reason that the data changes, is that the UITableViewCells get dequeued when you scroll them off the screen. The data is then gone at this point, and when you scroll back, the cell is repopulated with the initial values (not the changed ones). I don't know how to fix it, I am looking for that solution now. When I find it, I'll link you to it.

*EDIT***
Ok here is what you do:

First, you need to make your ViewController a delegate for UITextFieldDelegate. Then you need to implement:

  • (void)textFieldDidEndEditing:(UITextField *)textField

In my case, the user interface is done dynamically in code, so I Have an NSMutableDictionary that contains the names of all of the fields. So in my code I do the following:

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

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

If you have your text fields as properties then you'll need another way to update your storage variable. For instance, you might use a tag on the textField to identify which field.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文