2009-06-17 78 views
3

如果我有一個自定義UITableViewCell不使用嵌入到單元格中的textLabel,而是自己繪製圖形,如何在選擇時更改contentView的外觀,就像它自動爲默認文字(可通過設置selectedTextColor:進行自定義)?UITableViewCell的選擇顏色

如果我更改tableView:willSelectRowAtIndexPath:,那麼它只會在藍色選擇背景啓動後更新,但不會在動畫時更新,就像我想要的那樣。

回答

6

只是不要繼承UITableViewCell並使用默認行爲。 您可以在沒有任何子類的情況下完全自定義單元格。

閱讀this article瞭解更多詳情。

+0

這實際上就是我在做的事情,但是你的鏈接仍然指向了正確的方向。我需要爲UILabel更改突出顯示的文字顏色。 – 2009-06-17 05:04:47

3

如果你已經子類一個UITableViewCell,那麼你可以通過覆蓋以下定製單元格的內容:

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated { 
    if(highlighted) { 
     self.backgroundColor = [UIColor redColor]; 
    } else { 
     self.backgroundColor = [UIColor clearColor]; 
    } 

    [super setHighlighted:highlighted animated:animated]; 
} 
5

在你的tableview的cellForRowAtIndexPath方法添加此代碼,只需更改爲UITableViewCell的選擇款式預期顏色。

//------------------------------------------------------------------------- 
    //background selected view 
    UIView *viwSelectedBackgroundView=[[UIView alloc]init]; 
    viwSelectedBackgroundView.backgroundColor=[UIColor colorWithRed:124.0/255.0 green:202.0/255.0 blue:227.0/255.0 alpha:1.0]; 
    cell.selectedBackgroundView=viwSelectedBackgroundView; 
    //-------------------------------------------------------------------------