2009-10-01 59 views
10

當我將UITableViewCells backgroundColor設置爲半透明顏色時,它看起來不錯,但顏色不覆蓋整個單元格。UITableViewCell透明背景(包括imageView/accessoryView)

imageViewaccessoryView周圍的地區都上來爲[UIColor clearColor] ...

alt text

我試過明確設置cell.accessoryView.backgroundColorcell.imageView.backgroundColor爲彩色作爲細胞的backgroundColor相同,但它不起作用。它會在圖標周圍放置一個小框,但不會展開以填充左側邊緣。右邊緣似乎不受此影響。

我該如何解決這個問題?

編輯:這裏是原始表格單元代碼:

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
     cell.opaque = NO; 
     cell.textLabel.backgroundColor = [UIColor clearColor]; 
     cell.backgroundColor = [UIColor colorWithRed:.1 green:.1 blue:.1 alpha:.4]; 
     cell.textColor = [UIColor whiteColor]; 
    } 

    cell.imageView.image = [icons objectAtIndex:indexPath.row]; 
    cell.textLabel.text = [items objectAtIndex:indexPath.row]; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

    return cell; 
} 

回答

16

本和我今天想到了這一點,這裏是該集團的摘要,以防萬一,這會抓住其他人。

必須設置單元格背景和cell.textLabel.backgroundColor每次cellForRowAtIndexPath被調用時,不只是在alloc/init階段(即如果tableView有一個出列高速緩存未命中)。

所以,代碼變成這樣:

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
     cell.opaque = NO;   
    } 

    // All bgColor configuration moves here 
    cell.textLabel.backgroundColor = [UIColor clearColor]; 
    cell.backgroundColor = [UIColor colorWithRed:.1 green:.1 blue:.1 alpha:.4]; 
    cell.textColor = [UIColor whiteColor]; 
    cell.imageView.image = [icons objectAtIndex:indexPath.row]; 
    cell.textLabel.text = [items objectAtIndex:indexPath.row]; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

    return cell; 
} 
+1

其實我還得做1件事。 cell.textLabel.backgroundColor需要設置* AFTER *單元格的背景色。我不知道爲什麼我需要這樣做,但現在它工作。謝謝邁克! – 2009-10-03 14:24:10

2

你看着設定該單元的contentView的背景顏色,保持電池,配件和圖像視圖透明?

此外,this article可能會幫助您顯示一些替代方案。

+0

我用這篇文章的靈感,但不幸的是他不使用透明細胞。 設置contentView背景顏色有非常奇怪的效果。我也嘗試設置contentView.opaque = NO,但仍然看起來很糟糕。 – 2009-10-01 14:57:15