【问题标题】:UITextField placeholder text overlay issueUITextField 占位符文本覆盖问题
【发布时间】:2014-02-17 13:26:35
【问题描述】:

我有一个包含三个表格视图单元格的表格视图。 在单元格配置期间,我向每个单元格添加了一个 UITextField,并且我还在所有文本字段上设置了占位符值。 当表格视图加载时,最终结果如下所示:

我遇到的问题是,当我将任何单元格“从屏幕上”滚动出来时,它们再次出现时,占位符文本变得越来越暗,如下所示:

当我尝试通过在其中键入名称或以编程方式更改最后一个单元格上的占位符值来更改 UITextField 字符串值时,这些属性的旧值将保留,而新值将覆盖在单元格中,例如这个:

以下是负责配置这些单元的方法:

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

    // Configure the cell...
    [self configureCell:cell forIndexPath:indexPath];

    return cell;
}

- (void)configureCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath
{
    for (UIView *subview in cell.contentView.subviews) {
        [subview removeFromSuperview];
    }

    cell.backgroundColor = [UIColor whiteColor];
    cell.autoresizesSubviews = YES;
    cell.clearsContextBeforeDrawing = YES;
    CGRect textFieldFrame = cell.bounds;
    textFieldFrame.origin.x += 10;
    textFieldFrame.size.width -= 10;

    UITextField *textField = [[UITextField alloc] initWithFrame:textFieldFrame];
    textField.adjustsFontSizeToFitWidth = YES;
    textField.textColor = [UIColor lightGrayColor];
    textField.enabled = YES;
    textField.userInteractionEnabled = NO;
    textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    textField.clearsContextBeforeDrawing = YES;
    textField.clearsOnBeginEditing = YES;

    if (indexPath.section == ATTAddNewTimerSectionName) {
        textField.placeholder = @"Name";
        textField.userInteractionEnabled = YES;
        textField.delegate = self;
        textField.returnKeyType = UIReturnKeyDone;
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    } else if (indexPath.section == ATTAddNewTimerSectionDate) {
        textField.placeholder = @"Date/Time";
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    } else if (indexPath.section == ATTAddNewTimerSectionCalendar) {
        if (self.userCalendarEvent == nil) {
            textField.placeholder = @"See list of calendar events";
        } else {
            textField.placeholder = nil;
            textField.placeholder = self.userCalendarEvent.title;
        }
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    [cell addSubview:textField];
}

如您所见,我尝试了各种方法,例如在向单元格添加新视图之前删除所有子视图或在单元格和 UITextView 上设置 -[UIView setClearsContextBeforeDrawing:YES] 甚至将占位符值设置为没有成功。

任何指针将不胜感激!

【问题讨论】:

    标签: ios objective-c uitableview uitextfield


    【解决方案1】:

    你犯了一个很多人都会犯的经典错误。滚动表格时,单元格会被重复使用。

    正如所写,每次使用单元格时,您的代码都会不断创建和添加新的文本字段。因此,您会看到一个单元格中有多个文本字段的结果。

    您只想向单元格添加一个文本字段。更新您的代码,使其仅在文本字段不存在时添加它。

    您的代码似乎有逻辑问题。您将文本字段直接添加到单元格中,但您尝试将其从单元格的contentView 中删除。

    改变这个:

    [cell addSubview:textField];
    

    到:

    [cell.contentView addSubview:textField];
    

    【讨论】:

    • 是的,这就是问题所在。如果您看到文本与占位符重叠,这可能是因为在同一位置有 >= 2 个 UITextField
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    • 2016-05-10
    • 1970-01-01
    • 2012-01-21
    • 2010-11-23
    • 2016-02-21
    相关资源
    最近更新 更多