0

我在一个文本字段(称为 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
  }
}

我的控制台打印出“标志是”和“颜色应该是灰色...”,但文本没有改变。我是否错误地设置了文本?

4

1 回答 1

0

Lakesh 基本上是对的,我需要重新加载数据。

我通过向我的 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)的帮助!

于 2013-07-29T13:27:31.677 回答