UITextField 占位符文本覆盖问题 [英] UITextField placeholder text overlay issue

查看:31
本文介绍了UITextField 占位符文本覆盖问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a table view with three table view cells. During cell configuration, I add a UITextField to each of the cells and I also set the placeholder value on all of the text fields. When the table view loads the end results looks like this:

我遇到的问题是,当我将任何单元格离开屏幕"滚动时,占位符文本会变得越来越暗,如下所示:

The issue I'm having, is that when I scroll any of the cells "off the screen" when they reappear the placeholder text get darker and darker, like here:

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

When I then attempt to change the UITextField string value by typing a name in, or by programatically changing the placeholder value on the last cell, the old value of those properties stays, and the new value gets overlaid in the cell, like this:

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

Here are the methods responsible for configuring those cells:

- (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] 甚至将占位符值设置为没有成功.

As you can see, I have tried various things, like removing all the subviews before adding a new view to the cell or setting -[UIView setClearsContextBeforeDrawing:YES] on both the cell and the UITextView and even setting the placeholder value to nil without success.

任何指针将不胜感激!

推荐答案

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

You are making a classic error that so many people make. Cells get reused as a table is scrolled.

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

As written, your code keeps creating and adding a new text field every time the cell is used. So you are seeing the result of having multiple text fields in a cell.

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

You want to only add one text field to a cell. Update your code so it only adds the text field if it isn't there already.

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

It seems you have a logic problem in the code. You add the text field directly to the cell but you try to remove it from the cell's contentView.

改变这个:

[cell addSubview:textField];

到:

[cell.contentView addSubview:textField];

这篇关于UITextField 占位符文本覆盖问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆