简体   繁体   English

以编程方式设置UITextField中的文本不会从占位符的灰色更改颜色

[英]Programmatically setting text in UITextField not changing color from placeholder's gray

This has been annoying me for the last hour. 在过去的一个小时里,这一直困扰着我。 I have a UITableView with rows of custom cells. 我有一个带有自定义单元格行的UITableView。 Nothing fancy, just a UITextField for the label and a UITextField for editing. 没什么,只是标签的UITextField和编辑的UITextField。 However, there's one row which has the second UITextField's editing disabled and an accessory because pressing that brings up a date picker. 但是,其中一行禁用了第二个UITextField的编辑,并且有一个附件,因为按下该按钮会调出日期选择器。 The second UITextField in that cell has a placeholder set that says "Select date". 该单元格中的第二个UITextField具有一个占位符集,上面写着“选择日期”。 When the user selects the date from the picker, that text field gets updated. 当用户从选择器中选择日期时,该文本字段将被更新。 The update works, but the text color is the same gray as the placeholder's text. 更新有效,但文本颜色与占位符的文本相同。

Code: 码:

                if(selectedDate == nil)
                    cell.cellTextField.placeholder = @"Select date";
                else
                {
                    cell.cellTextField.text = selectedDate;
                }

I can't figure out how to get the UITextField's color to be black. 我不知道如何使UITextField的颜色为黑色。 If you scroll that row below the bottom of the view and bring it back, THEN it's black. 如果在视图底部下方滚动该行并将其放回原处,则它为黑色。

I tried setting UIColor to black. 我尝试将UIColor设置为黑色。 No good, and not necessary since the color is correct after scrolling. 不好,也没有必要,因为滚动后颜色正确。

I also tried setNeedsDisplay. 我也尝试过setNeedsDisplay。 That didn't work either. 那也不起作用。

Everything I tried from the docs doesn't work, and yet I can't figure out why iOS is setting the color of the .text to the same color as the .placeholder 我在文档中尝试过的所有方法均无法正常运行,但我无法弄清楚为什么iOS会将.text的颜色设置为与.placeholder相同的颜色

Any ideas? 有任何想法吗?

Your description of scrolling off screen then back on implies the following might work: 您对从屏幕上向下滚动然后再重新打开的描述可能适用于以下情况:

Try: 尝试:

[tableView reloadData];

after you have updated the text in your tableView cell (presumably right after the picker has notified you of the pick). 更新tableView单元中的文本之后(大概是在选择器将选择通知给您之后)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM