我在文本字段(称为countTotalFieldUniversal)中有占位符文本,当我单击“编辑”按钮时,我想要更改颜色。
目前,我的代码是:
- (void)setEditing:(BOOL)flag animated:(BOOL)animated
{
NSLog(@"setEditing called");
[super setEditing:flag animated:animated];
if (flag == YES){
NSLog(@"Flag is yes");
[countTotalFieldUniversal setValue:[UIColor darkGrayColor]
forKeyPath:@"_placeholderLabel.textColor"];
NSLog(@"Color should be grey...");
}
else {
// Edit not selected
}
}
我的控制台打印出“Flag is yes”和“Color应该是灰色......”但文本没有改变。 我是否错误地设置了文字?
答案 0 :(得分:0)
Lakesh大部分都是对的,我需要reloadData。
我通过在setEditing函数中添加一个布尔值(editClicked)来改变文本颜色:
- (void)setEditing:(BOOL)flag animated:(BOOL)animated
{
NSLog(@"setEditing called");
[super setEditing:flag animated:animated];
if (flag == YES){
editClicked = true;
[tableView reloadData];
}
else {
editClicked = false;
}
}
在我的表的cellForRowAtIndexPath函数中,我添加了以下if语句:
//If editClicked is true, change text to grey
if (editClicked == true){
[countTotalFieldUniversal setValue:[UIColor darkGrayColor]
forKeyPath:@"_placeholderLabel.textColor"];
}
else {
// Save the changes if needed and change the views to noneditable.
[countTotalFieldUniversal setValue:[UIColor blackColor]
forKeyPath:@"_placeholderLabel.textColor"];
}
感谢大家(特别是Lakesh和Kyle)的帮助!