2010-06-29 80 views
1

我試圖覆蓋自定義UITableViewCell子類中的-selectedBackgroundView的訪問器。但是,單元格被選中時,單元格將繼續使用默認的藍色選擇背景。如何改變?如何更改UITableViewCell的藍色選擇?

我其實很確定這個藍色的選擇是selectedBackgroundView ...還有什麼應該對這個具有淺色漸變的藍色背景負責?但奇怪的是,contentView高於此值,雖然它具有除[UIColor clearColor]之外的backgroundColor,但這個selectedBackgroundView仍然似乎閃耀着光芒。真奇怪。

回答

7

你只是把這個代碼放到「的cellForRowAtIndexPath」

對於禁用的細胞中進行選擇:(同時單擊單元格)

cell.selectionStyle = UITableViewCellSelectionStyleNone; 

爲了使細胞:(同時單擊單元格)

cell.selectionStyle = UITableViewCellSelectionStyleBlue;(By Default) 

cell.selectionStyle = UITableViewCellSelectionStyleGray; 

如果你想給你的自定義顏色,然後在的cellForRowAtIndexPath」

if (cell==nil) 

{ 
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease]; 
UIView *v = [[[UIView alloc] init] autorelease]; 
    v.backgroundColor = [UIColor redColor]; 
    cell.selectedBackgroundView = v; 
} 

一切順利。

1

聽起來像是你只是想

self.selectionStyle = UITableViewCellSelectionStyleNone; 

添加到您的自定義類的初始化。如果你想做任何重型處理,你也可以重寫setSelected:animated:方法。

+0

它應該是cell.selectionStyle = UITableViewCellSelectionStyleNone;而不是self.selectionStyle = UITableViewCellSelectionStyleNone; – 2012-05-01 11:42:38