2009-06-17 57 views
68

我試圖將UITableViewCell的背景顏色設置爲透明。但似乎沒有任何工作。我在tableViewCell中有一些圖像/按鈕,我想讓背景消失以獲得圖像和按鈕的「浮動」效果(就好像它們不在tableview中一樣)。如何使用透明背景創建UITableViewCell

任何想法如何實現?

回答

120

如果兩個其他選項不起作用試試這個

UIView *backView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease]; 
backView.backgroundColor = [UIColor clearColor]; 
cell.backgroundView = backView; 
+14

myTable.backgroundColor = [UIColor clearColor];爲我工作!謝謝 – sjobe 2009-11-05 14:21:14

+7

這也適用於我,雖然我還必須使用: cell.backgroundColor = [UIColor clearColor]; – 2009-11-10 07:27:07

30

開始與這些文章,以獲得背景色正確設置:

http://howtomakeiphoneapps.com/2009/03/how-to-add-a-nice-background-image-to-your-grouped-table-view/

http://pessoal.org/blog/2009/02/25/customizing-the-background-border-colors-of-a-uitableview/

然後嘗試以下所有行:

[[cell contentView] setBackgroundColor:[UIColor clearColor]]; 
[[cell backgroundView] setBackgroundColor:[UIColor clearColor]]; 
[cell setBackgroundColor:[UIColor clearColor]]; 

有比只是多了一個隱藏在UITableViewCell中的視圖。這應該使你的方式清晰。

+0

作品完美... 除了一個錯字:setBbackgroundColor => setBackgroundColor – 2010-05-03 22:40:04

+1

cell.contentView的backgroundColor對我來說似乎是真正的關鍵。 (僅在iOS7上確認) – bitwit 2014-02-28 23:00:24

+0

你真棒。 +100如果我可以 – Katushai 2014-06-03 21:03:54

2

我在我的自定義tableviewcell和backgroundImage用白色標籤背景疊加的問題,我都試過了,最後設置背景色清在看法會有所斬獲。

 - (void)viewWillAppear:(BOOL)animated 
    { 
[super viewWillAppear:animated]; 
    self.tableView.backgroundColor = [UIColor clearColor]; // if this is not here, cell background image is covered with a white rectangle :S 
    } 

,並在rowForCellAtIndexPath我設置背景圖片:

if(!cell) { 
    cell = [[[UITableViewCell alloc] initWithFrame: ... 
    UIImageView *cellBG = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellbg.jpg"]]; 
    [cell setBackgroundView:cellBG]; 
    [cellBG release]; 
} 
40

這UITableViewCell中的文檔實際上是回答。因此,雖然您必須在控件上設置透明度,但實際單元格背景顏色必須在每個下方設置爲

「注意:如果你想改變一個單元格的背景色(通過設置經由UIView的聲明backgroundColor屬性單元格的背景顏色),你必須這樣做在的tableView:willDisplayCell:forRowAtIndexPath:方法,而不是在數據源的tableView:cellForRowAtIndexPath:中改變組風格表格視圖中單元格的背景顏色在iOS 3.0中的作用與以前版本的操作系統不同,它現在影響圓角矩形內的區域,而不是其外部的區域。「

https://developer.apple.com/documentation/uikit/uitableviewcell

11

默認情況下你的UITableViewCell的背景是白色的。做myCell.backgroundColor = [UIColor clearColor];會使你的UITableViewCell透明,但你不會感覺到差異,因爲UITableView(你的UITableViewCell的競爭者)在默認情況下也有白色背景。

如果你想看看你的UIView的背景,你將不得不讓你的細胞,你的表格背景透明:

myTableView.backgroundColor = [UIColor clearColor]; 
myTableCell.backgroundColor = [UIColor clearColor]; 

馬丁Magakian

25
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    cell.backgroundColor = [UIColor clearColor]; 
    cell.backgroundView.backgroundColor = [UIColor clearColor]; 
} 
17

我有一個簡單的解決方案

Objective-c版本:

cell.backgroundColor = [UIColor clearColor]; 

斯威夫特版本:

cell.backgroundColor = UIColor.clearColor() 
3

如果您使用的故事板,確保取消選中 「不透明」 爲原型UITableViewCells