【问题标题】:Values entered in custom UITableViewCell UITextField translated to other fields在自定义 UITableViewCell UITextField 中输入的值转换为其他字段
【发布时间】:2015-06-12 15:17:47
【问题描述】:

所以,这将是一个非常奇怪的问题。我会尽量具体。

这是我的 UITableViewController 中的一个 sn-p:

 - (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 也仍然持有该值。当我离开屏幕并重新进入时,两个文本字段都保留了该值。

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

【问题讨论】:

    标签: ios objective-c uitableview ipad


    【解决方案1】:

    正常分配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";
    

    【讨论】:

    • 我根据其他用户的回答还原了所做的更改,这解决了我的问题。感谢您的帮助。
    【解决方案2】:

    表格视图重复使用单元格。当一个单元格滚动到屏幕外时,它会被添加到一个队列中,并将被重新用于下一个要在屏幕上滚动的单元格。这意味着您在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;
    

    【讨论】:

    • 这只会加剧问题。现在这些值都从一个单元格转换到另一个单元格,而且它们也在随意消失。我明白你在说什么,但无论出于何种原因,它都不起作用。
    • 那么我认为您的代码中其他地方还有第二个问题,可能在您的模型中或在更新模型时。
    猜你喜欢
    • 2016-10-05
    • 2017-08-25
    • 2011-05-21
    • 1970-01-01
    • 2011-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-07
    相关资源
    最近更新 更多