2013-07-29 39 views
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 
    } 
} 

我的控制檯打印出的是「國旗是yes」和「顏色應該是灰色的......」但文字不會改變。 我是否正確設置文本?

+0

您可能需要告訴現場才能拿起你的鍵/值更改爲重繪:'[self.countTotalFieldUniversal setNeedsDisplay]'。 –

+0

我補充說,在以前的NSLog權(@「顏色應該是灰色的......」);,但它並沒有改變。但是,我不得不[countTotalFieldUniversal setNeedsDisplay]。它不會接受self.countTotalFieldUniversal ...... – AllieCat

+0

我同意你的看法,我需要「刷新」,以表視圖。我只是不知道如何... – AllieCat

回答

0

Lakesh主要是對的,我需要reloadData。

我通過增加一個布爾(editClicked)我setEditing功能改變文字顏色:

- (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)的幫助!