【问题标题】:Having issue with UITextField and UITableViewCellsUITextField 和 UITableViewCells 有问题
【发布时间】:2012-01-05 02:54:14
【问题描述】:

我的UITableViewCell 中有一个UITextField,在一个UITableView 中。我总共有 11 个单元格。当我在单元格中输入文本并将单元格滚动到屏幕外时,该文本将被清除。

似乎单元格正在出队或解除分配。只有 11 个,所以问题不大,但我怎样才能让所有单元格的 textFields 在此视图中始终可见?

这是我正在使用的代码。是因为我没有诸如数组之类的数据源吗?如果服务器有文本,我将从childAppointmentDictionary 中提取文本,否则用户输入文本并将其保存到NSString

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CellIdentifier = @"textCell";
    VitalsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    switch (indexPath.row) {
        case 0:
            cell.vitalsLabel.text = @"Temperature";
            cell.textField.text = [self.childAppointmentDictionary objectForKey:@"temperature"];
            self.temperatureTextField = cell.textField;
            break;
        case 1:
            cell.vitalsLabel.text = @"Pulse";
            cell.textField.text = [self.childAppointmentDictionary objectForKey:@"pulse"];
            self.pulseTextField = cell.textField;
            break;
        default:
            break;
    }
    return cell;
}

- (void) textFieldDidEndEditing:(UITextField *)textField{
    if (textField == temperatureTextField){
        self.temperatureString = self.temperatureTextField.text;
    } 
    else if (textField == pulseTextField){
        self.pulseString = self.pulseTextField.text;
    }
}

【问题讨论】:

  • 你能显示你的cellForRowAtIndexPath方法的代码吗?
  • 我可以使用UIScrollView 复制这个UITableView 的外观吗?这会解决我遇到的一些问题吗?

标签: iphone memory-management uitableview uitextfield


【解决方案1】:

屏幕外的单元格可以重复使用。这是正常的行为。您需要填写正确的方法以确保其内容得到刷新。

http://developer.apple.com/library/IOs/#documentation/UIKit/Reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:willDisplayCell:forRowAtIndexPath:

正如 Gabriele 指出的,您需要使用 tableView:cellForRow: 来替换文本。

【讨论】:

  • 我应该将每个单元格的textField.text 添加到NSDictionary 还是NSArray
  • 单元格中需要的文本需要存储在其他地方,以便在 forRowAtIndexPath 中重复使用,或者在 forRowAtIndexPath 函数中重新生成。
  • 好的,现在初始文本存储在childAppointmentDictionary 对吗?如果我根据我的代码添加没有存储在任何地方的编辑文本,它只是新文本,对吗?
  • 好的,所以我将创建一个新字典并让它使用相同的键,如果用户输入文本,请将其添加到新字典中的该键中。那应该有效吗?我将在哪里实际放置代码来执行此操作?喜欢self.myDictionary = [self.temperatureTextField.text objectForKey: @"temperature"];
  • 我不太确定你应该在哪里这样做。问题的重点是处理您消失的文本。因此,正如 Gabriele 所说,您需要设置 tableView:cellForRow: 才能将文本放回原处。存储文本的位置超出了此问题的范围。
【解决方案2】:

当单元格关闭屏幕时,出于性能原因,内部的所有视图都将被释放。它不存储您的文本(NSString),当单元格进入屏幕时,TableView 会重绘它但没有文本。

您的文本 (NSString) 必须被视为您的模型。所以在 tableView:cellForRow: 中,每次都需要将你想要的 NSString 设置为文本。

一些代码

您的方法几乎是好的,但您需要使用 cellIdentifier 分配单元至少 1 次。单元格需要使用文本字段或类似字段进行子类化,就像我想的那样,并使用 dequeueReusableCellWithIdentifier: 从 tableView 中检索单元格模板。 检索单元格模板后,您就可以为文本字段配置文本。每次 tableView:cellForRowAtIndexPath 使用存储在模型中的 NSString 调用 tableView:cellForRowAtIndexPath 时都会填充此文本字段(在您的情况下为 NSDictionary)。 如果您要为表分层静态行数,我建议您使用枚举以使代码更具可读性。我建议也阅读A Closer Look at Table-View Cells。这是非常非常有用的。 希望对您有所帮助。

typedef enum {
    kCellRowTemperature = 0,
    kCellRowPulse
} CellRowTAG;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier = @"textCell";
    VitalsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[VitalsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    switch (indexPath.row) {
        case kCellRowTemperature:
            cell.vitalsLabel.text = @"Temperature";
            cell.textField.text = [self.childAppointmentDictionary objectForKey:@"temperature"];
            self.temperatureTextField = cell.textField;
            break;
        case kCellRowPulse:
            cell.vitalsLabel.text = @"Pulse";
            cell.textField.text = [self.childAppointmentDictionary objectForKey:@"pulse"];
            self.pulseTextField = cell.textField;
            break;
        default:
            break;
    }
    return cell;
}

【讨论】:

  • 您能否根据我在问题中的代码举例说明如何执行此操作?
  • 我应该将每个单元格的 textField.text 添加到 NSDictionary 还是 NSArray?
猜你喜欢
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多