2009-09-28 49 views
24

我需要將表格視圖的默認藍色選擇更改爲某種自定義顏色。有沒有辦法做到這一點。幫我需要更改表格視圖選擇的顏色

+12

我想你會得到更好的結果,如果你添加更多的驚歎號... – 2009-10-30 12:38:24

回答

62

做到這一點,最好的辦法是這樣的:

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"myCellId"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     UIView *v = [[[UIView alloc] init] autorelease]; 
     v.backgroundColor = [UIColor redColor]; 
     cell.selectedBackgroundView = v; 
    } 
    // Set up the cell... 
    cell.textLabel.text = @"foo"; 
    return cell; 
} 

對您的相關部分是cell.selectedBackgroundView = v;指令。 你可以在這裏用你喜​​歡的任何視圖替換非常基本的視圖'v'。

+1

卓然,我已經驗證了這種方法適用於普通表格視圖,但它不適用於我的分組表格。任何解決方法?或者你可以證實這一點? – 2009-09-28 13:07:28

+0

它不依賴於表視圖類型:我剛剛發現它可以在一些普通表上使用,並且不會在其他普通表上使用。他們都使用自定義表格單元格。 – 2009-09-28 13:22:23

+1

這隻適用於普通表格視圖無模糊。對於分組表格視圖,實際需要4個視圖來選擇不同的背景;最頂層,最底層,在另外兩個單元之間,以及組中的單個單元。然後更新這些更改您在任何部分中更改行數的痛苦。在bugreport.apple.com上發佈功能請求,爲iPhone OS 4.0添加「selectedBackgroundGradient」屬性。 – PeyloW 2009-09-28 14:56:05

3

我不認爲你可以使用自定義顏色。但是,你可以使用的UITableViewCell

@property(nonatomic) UITableViewCellSelectionStyle selectionStyle 

選擇的風格是backgroundView常數,確定細胞的顏色選擇,當它的下列財產。默認值是UITableViewCellSelectionStyleBlue。由於

typedef enum { 
    UITableViewCellSelectionStyleNone, 
    UITableViewCellSelectionStyleBlue, 
    UITableViewCellSelectionStyleGray 
} UITableViewCellSelectionStyle; 

您可以從默認的藍色切換到灰色,或根本沒有彩色選擇。

+1

如果你繼承你的UITableViewCell,你可以突出顯示任何顏色。 – mahboudz 2009-09-28 07:10:58

+0

您是如何爲使用IB或編程設計的自定義UITableViewCell執行此操作的?我的意思是,是否有任何方法/屬性允許這樣做,而不會重複使用諸如將另一視圖放在單元格之上的技巧? – 2009-09-28 07:59:24

+0

你可以給我發送UITableViewCell子類的代碼並用自定義顏色突出顯示它。 please .. – smakstr 2009-09-28 08:42:28

0

這樣做的另一種方法是在你的單元格中移動一個新的視圖,使用任何你想要的顏色和50%左右的不透明度。當您收到-setSelected:animated:call時,將此視圖移至單元格。當我說移動時,你實際上可以始終在單元格的頂部有一個視圖,但只需將隱藏位關閉並根據需要打開即可。

6

我也遇到過這個問題,試圖爲分組的單元格創建自定義的選定單元格視圖。我通過爲細胞頂部,中間和底部創建3種類型的圖像來解決這個問題,假設會有一箇中間細胞。

NSString *imageFile; 

if (row == 0) { 
imageFile = @"highlighted_cell_top.png"; 
} else if (row == ([registeredDetailsKeys count] - 1)) { 
imageFile = @"highlighted_cell_bottom.png"; 
} else { 
imageFile = @"highlighted_cell_middle.png"; 
} 

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageFile]]; 

cell.selectedBackgroundView = imageView; 

有可能是一種更簡單的方法,但我很滿意的結果。

+0

據我所知,這正是要做到這一點。看到這個[教程](http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html)。你已經忘記了「top_bottom」的變化(當只有一個單元格時),就是這樣。 – Rich 2012-02-01 16:58:04

3

確保您在頭後聲明

@property(nonatomic) UITableViewCellSelectionStyle selectionStyle 

實施

cell.selectionStyle = UITableViewCellSelectionStyleGray 

其中UITableViewCellSelectionStyleGray可以UITableViewCellSelectionStyleNoneUITableViewCellSelectionStyleBlueUITableViewCellSelectionStyleGray