【问题标题】:UITableView Inserting Old DataUITableView 插入旧数据
【发布时间】:2013-01-11 15:04:38
【问题描述】:

我有一个带有 1 部分的简单 UITableView,其中存在任意数量的带有 UITextFields 的自定义 UITableViewCells(KMInputCell 类型)的行。当用户开始在最后一个(空白)文本字段中输入时,会插入一个新的空白行(因此用户可以创建一个类似列表的结构)。由于我只会在视图关闭时“保存”数据,因此数据源只是一个 NSUInteger 跟踪行数。

在用户从表中删除一行之前,我的代码可以正常工作。然后,当用户开始在列表末尾键入并应插入新(空白)行时,插入的 UITableView 单元格包含已删除单元格中的旧数据。更糟糕的是,当多行被删除,然后用户开始输入(并且应该插入一个空白行)时,多行被删除的行会突然出现。

这是在编辑单元格中的一个 UITextField 时调用的 fieldChanged: 方法(其中self.last_cell 返回该部分中的最后一个单元格):

- (IBAction)fieldChanged:(id)sender {
    // get the text field of the last row
    // if it has a value that is not blank, insert another row
    KMInputCell* last_cell = self.last_cell;
    if(![last_cell.textField.text isEqualToString:@""]){
        [self.tableView beginUpdates];
        NSIndexPath* new_cell_path = [NSIndexPath indexPathForItem:[self.tableView numberOfRowsInSection:0] inSection:0];
        [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:new_cell_path] withRowAnimation:UITableViewRowAnimationAutomatic];
        number_emails++;
        [self.tableView endUpdates];
    }
}

这是用于删除单元格的commitEditingStyle: 方法:

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        number_emails--;
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
     }   
}

加法

这里是cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"add_email_prototype";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];=

    // Configure the cell...

    return cell;
}

【问题讨论】:

  • 如果您的“数据源”只是一个 int,那么您将“稍后保存”的文本存储在哪里?这绝对不是表格视图所期望的那种数据源。你能展示你的cellForRowAtIndexPath委托方法吗?
  • 我会把cellForRowAtIndexPath放在上面。我没有在数据源中提供数据的原因是数据仅由用户在单元格的 UITextFields 中提供。
  • 正如艾萨克所说,你需要清除单元格;在 Isaac 发表评论的地方插入代码。此外,您绝对应该考虑使用某种数据模型。你的方法会失败。
  • 正如我在他的回答中评论的那样,由于用户管理 UITextFields 中的数据,如果删除单元格,这不会使事情变得复杂吗?那么是不是很难弄清楚要删除哪个数组元素等?​​
  • 这并不复杂 - 只需查看表格视图数据源和委托的文档即可。

标签: iphone ios objective-c uitableview


【解决方案1】:

我不太清楚你的数据模型的细节......仅仅跟踪一个整数并不是一个很好的方法 - 但也许我只是不明白你在做什么。您是否有代表这些行的模型数组或模型对象,或者您只是在跟踪其字段中的文本?

您正在插入新行,但问题可能与 UITableViewCell 在呈现时被重用的事实有关。如果这确实是这里的问题,您可能需要更新您在此处未提供的方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...

    return cell;
}

这应该是您用来填充文本字段的方法。也有必要明确清除任何动态字段的内容,以便在重新使用表格单元格时,您不会在其中得到意外的内容。

更新

文本字段本身不适合保留数据。正如您从这里的问题中看到的那样,该体系结构将其排除在外,因为单元格在屏幕上下滚动(或被删除/添加)时被重复使用。一种做法是创建一个数组并将这些文本字段的内容作为字符串存储在其中。

【讨论】:

  • 重点是我只是在跟踪字段中的文本——我没有代表行的模型,因为它都是由用户输入的。
  • 除非您设置一个简单的数据模型,否则您几乎肯定会继续遇到困难。但即使你不这样做,我给出的答案也可能会在短期内解决你描述的问题。您需要清除 cellForRowAtIndexPath 中的文本字段。
  • 啊,我认为当我清除 cellForRowAtIndexPath 中的文本字段时它起作用了。如果我设置一个数据模型,如果删除一个单元格,事情会不会变得复杂?例如,我必须弄清楚要从数组中删除哪个元素。
  • 这可能比您想象的要容易。你只需要设置一个 NSMutableArray,其中每个对象都有一个索引。由于您的 tableview 为您提供回调(例如,当它想要显示项目时 cellForRowAtIndexPath 或 tableView:commitEditingStyle:forRowAtIndexPath: 当添加或删除项目时),您可以使用 indexPath.row 将该行与您的数组匹配。这是一个简单的介绍,演示:iphonesdkarticles.com/2009/01/…
  • 谢谢,我去看看。
【解决方案2】:

在 cellForRowIndex 函数中,只需将它们初始化为 nil。这将解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-14
    • 2020-09-17
    • 2021-04-27
    • 1970-01-01
    • 2013-01-09
    • 2011-06-06
    • 1970-01-01
    • 2013-06-17
    相关资源
    最近更新 更多