2011-09-29 57 views
4

我想創建一個TRANSLUCENT分組表格視圖單元格。換句話說,我想查看分組表格視圖的背景圖案,但我不想要完全清晰的單元格。我已經看到很多關於透明單元的問題,但都沒有提到半透明單元(只有部分透明)。半透明UITableViewCell分組的UITableView?

這就是我想:

- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    cell.contentView.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.5f]; 
    cell.backgroundColor = [UIColor clearColor]; 
    cell.backgroundView.backgroundColor = [UIColor clearColor]; 
} 

這是結果: enter image description here

這幾乎是正確的,但細胞的內容查看超出分組的​​圓角細胞。

解決方案通過使用透明圖像並設置單元格的背景視圖。本來仍然喜歡以編程的方式來做,所以如果任何人有解決方案,我會很樂意接受它。

解決的第二部分也可以由backgroundView設置到一個新的UIView,有通過QuartzCore的setCornerRadius調用視圖的圖層屬性的背景顏色和圓角來完成。

+0

的backgroundView屬性接受一個UIView所以纔沒有必要使用圖像。只需用你想要的顏色創建一個視圖,並將其alpha設置爲適合你的透明度即可。 – Rog

+0

這與我最初嘗試的基本相同(將backgroundView分配給新的UIView或直接將顏色直接分配給它沒有區別)。沒有圓角。 – Keller

+0

但是,它發生在我身上,它可以通過QuartzCore setCornerRadius調用:) – Keller

回答

3

爲別人想,我結束了使用的代碼是:

- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    //... 

    cell.contentView.backgroundColor = [UIColor clearColor]; 
    cell.backgroundColor = [UIColor clearColor]; 
    UIView *bgView = [[UIView alloc] init]; 
    [[bgView layer] setCornerRadius:10.0f]; 
    [bgView setBackgroundColor:[UIColor colorWithWhite:1.0f alpha:0.25f]]; 
    cell.backgroundView = bgView; 

    //... 

    return cell; 
} 
+0

這隻適用於如果你有一節中的一個單元格。否則看起來古怪。 –

+0

這對我來說非常合適,並且在-tableView:heightForRowAtIndexPath中添加了一些額外的高度:以前版本的代碼每個數據項都使用了一個部分,只是爲了利用內置透明度節! – Endareth

3

這應該是所有你需要:

cell.contentView.backgroundColor = [UIColor clearColor]; 
cell.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.5]; 
+0

這是正確的解決方案。另外你應該設置textLabel backgroundColor爲clearColor:cell.textLabel.backgroundColor = [UIColor clearColor]; –