【问题标题】:Characters not cleared in a UITextField inside a UITableViewCellUITableViewCell 内的 UITextField 中未清除的字符
【发布时间】:2012-01-17 09:41:27
【问题描述】:

我使用表格来编辑模型对象(注释实例)的属性。表格中的单元格对应Annotation类的不同属性:

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellAnnotation"];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2  reuseIdentifier:@"cellAnnotation"] autorelease];
    }
    [[cell textLabel] setText:@"Category"];
    categoryField = [[UITextField alloc] initWithFrame:CGRectMake(90, 8, 200, 25)];
    [categoryField setAutocorrectionType:UITextAutocorrectionTypeNo];
    [categoryField setFont:[UIFont fontWithName:@"Verdana" size:15]];
    [categoryField setText:[[annotation category] name]];
    [categoryField setDelegate:self];
    [[cell contentView] addSubview:categoryField];
    [categoryField release];
    return cell;

令人惊讶的是,如果我在控制器的 viewWillAppear 方法中重新加载数据,那么我会得到一个奇怪的行为:按下退格键时 UITextField 中的字符不会被清除,尽管在退出第一响应者时它们不存在在 UITextField 中并检索其文本属性。谁能帮我?提前致谢。

【问题讨论】:

    标签: ios uitableview uitextfield


    【解决方案1】:

    您不应将子视图添加到 if (cell == nil) { ... } 子句之外的表格单元格。表格单元格被重新循环,因此每次调用 cellForRowAtIndex 时,您都会将该子视图再次添加到表格单元格中,而不会删除旧的。这可能是文本看起来没有被清除的原因,因为您将多个字段添加到单元格中。

    查看我写的另一个答案以获得更详细的解释:

    cellForRowAtIndexPath memory management

    【讨论】:

    • 非常感谢尼克。这很简单,但不知何故我错过了重点。
    猜你喜欢
    • 2019-11-27
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    • 2014-11-23
    • 1970-01-01
    • 2011-09-19
    相关资源
    最近更新 更多