2012-05-19 156 views
10

我有一個自定義UITableViewCell。它內有3個自定義標籤,並帶有自定義文本。自定義表格視圖單元格選擇字體顏色

當我點擊單元格時,我想讓所有這些標籤的textColor變白。就像電子郵件應用UITableViewCell的行爲一樣。

爲此,我在自定義單元格類中編寫了此代碼。

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 

    if (self.selected) { 
     _subjectLabel.textColor = [UIColor whiteColor]; 
     _messageLabel.textColor = [UIColor whiteColor]; 
     _usernameLabel.textColor = [UIColor whiteColor]; 
    }else { 
     _subjectLabel.textColor = [UIColor blackColor]; 
     _messageLabel.textColor = [UIColor grayColor]; 
     _usernameLabel.textColor = [UIColor blackColor]; 
    } 



} 

我能夠得到它。但它不像電子郵件應用程序那樣流暢。顏色只會在一小段延遲後纔會改變。我應該重寫哪一種UITableViewCell的方法來放入此代碼。我瞭解以下選項,但它們不會將行爲賦予自定義單元格中的自定義標籤。

typedef enum { 
    UITableViewCellSelectionStyleNone, 
    UITableViewCellSelectionStyleBlue, 
    UITableViewCellSelectionStyleGray 
} UITableViewCellSelectionStyle; 

回答

23

設置標籤的高亮文字顏色,這一切都將自動完成。你根本不需要做任何特別的事情,setSelected

例如

_subjectLabel.highlightedTextColor = [UIColor whiteColor]; 
+0

謝謝。有效! – Anand

+0

只需稍微提醒一下,如果單元格是自定義單元格,則可以打開xib並在屬性檢查器下,可以設置「突出顯示」顏色。 –

1

當我們選擇UITableView任何細胞,-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath方法立即調用,您可以使用此。

+0

' - (空)的tableView:(UITableView的*)的tableView didSelectRowAtIndexPath方法:(NSIndexPath *)indexPath'火災,只有當你把你的手指出細胞的..不是當您把它..我想顏色來改變當我將手指放在單元格上的時刻..看看電子郵件應用程序例如 – Anand

+0

在這種情況下,您可以使用touchBegan方法,只要您將手指放在單元格上,就會調用該方法,目前您使用的是哪種方法使用? – rishi

+0

目前上面的代碼有效。但有一個小的延遲 – Anand

相關問題