在自定义 UITableViewCell UITextField 中输入的值转换为其他字段

Values entered in custom UITableViewCell UITextField translated to other fields

因此,这将是一个需要解决的非常奇怪的问题。我会尽量具体。

这是我的 UITableViewController 的一个片段:

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

    static NSString *cellIdentifier = @"miscCell";
    JASMiscConfigurationTableViewCell *cell = ((JASMiscConfigurationTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]);
    if (cell == nil) {
        cell = [[JASMiscConfigurationTableViewCell alloc] init];
    }

    JASMiscellaneous *misc = [((NSMutableArray *)[_miscellaneousList objectAtIndex:indexPath.section]) objectAtIndex:indexPath.row];
    [cell.itemNameLabel setText:misc.itemDescription.productCostDescription];
    if ([misc.itemQuantity doubleValue] > 0) {
        [cell.itemQuantityField setText:[misc.itemQuantity stringValue]];
    }

    return cell;

}

JASMiscConfigurationTableViewCell 只是一个带有标签和文本字段的自定义 UITableViewCell。

这里手头的问题是:

如果我在单元格的 UITextField 中输入一个值并向下滚动页面,则输入的值会在我滚动时按字面意思向下移动页面。当我停止滚动时,它总是设法直接停在另一个行单元格的 UITextField 内。输入的值在离开其原始 UITextField 时不会消失,它会漂浮在屏幕的最前端。这也不仅仅是一个 GUI 错误。当我遍历单元格以获取它们的值以将它们存储在对象中时,值已转换为的 UITextField 实际上保存了该值。更奇怪的是,输入值的原始 UITextField 也仍然保留该值。当我离开屏幕并重新进入时,两个文本字段都保留了值。

如果这听起来令人困惑,我很抱歉。这让我很困惑。如果您需要任何说明,我很乐意提供。感谢帮助。

Table 观看重复使用单元格。当一个单元格滚动到屏幕外时,它被添加到一个队列中,并将被重新用于下一个要滚动到屏幕上的单元格。这意味着您在 tableView:cellForRowAtIndexPath: 方法中的配置代码将在不同索引路径的同一单元格上再次 运行 。

这意味着您需要在每次 这 运行 秒更新 itemQuantityField 的文本 ,而不仅仅是当您的数量大于零时。否则,该单元格仍将具有之前在不同索引路径中使用时的文本。

我已经重写了您的 if ([misc.itemQuantity doubleValue] > 0) {...},如果 itemQuantity 为零或更小,文本将设置为 nil。使用 else 子句也可以实现同样的效果。

BOOL validQuantity = [misc.itemQuantity doubleValue] > 0;
cell.itemQuantityField.text = validQuantity ? [misc.itemQuantity stringValue] : nil;

正常分配 cell = [[JASMiscConfigurationTableViewCell alloc] init]; 不同于 -reuseIdentifier 分配,例如..

cell = [[JASMiscConfigurationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

有可能

JASMiscConfigurationTableViewCell *cell = ((JASMiscConfigurationTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]);

继续使用上一个单元格。

试试这个:

static NSString *cellIdentifier = @"miscCell";
JASMiscConfigurationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]);
if (cell == nil) {
    cell = [[JASMiscConfigurationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

关于held/replaced when the cell leaves and reenters view

您缺少 else 语句:

if ([misc.itemQuantity doubleValue] > 0) {
    [cell.itemQuantityField setText:[misc.itemQuantity stringValue]];
}
//set value if the condition is not met.
cell.itemQuantityField.text = @"no";