2011-11-07 64 views
4

我有下面的代碼,創建一個UIView,我分配給我的UITableViewCellselectedBackgroundView屬性。一切都按預期工作,子視圖的背景是透明的。UITableViewCell自定義selectedBackgroundView背景是透明的

我使用相同的代碼來創建自定義視圖,我將其分配給backgroundView,並且工作正常。

什麼導致該子視圖對於selectedBackgroundView是透明的,我該如何避免這種情況?

- (UIView*) makeSelectedBackgroundView 
{ 
    // dimensions only for relative layout 
    CGRect containerFrame = CGRectMake(0, 0, 320, 40); 

    UIView* containerView = [[UIView alloc] initWithFrame:containerFrame]; 
    containerView.autoresizesSubviews = YES; 

    // dimensions only for relative layout 
    CGRect subframe = CGRectMake(5, 5, 310, 30); 
    UIView* subview = [[UIView alloc] initWithFrame:subframe]; 

    subview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 

    subview.backgroundColor = [UIColor redColor]; 
    subview.layer.cornerRadius = 5; 
    subview.layer.borderWidth = 2; 
    subview.layer.borderColor = [UIColor greenColor].CGColor; 

    [containerView addSubview:subview]; 

    return containerView; 
} 
+0

你可以添加一張你得到的圖片和一張你期望的圖片嗎? – alinoz

+0

你試過玩阿爾法?子視圖的[subview setAlpha:0.0]。 – alinoz

回答

-1

最後,我結束了包含自定義視圖對象的UITableViewCell的子類化,並且工作。

+3

而這又如何解決問題呢? – esbenr

-1

嘗試setOpaque:YES在您的意見。

+0

這沒有幫助。 –

0

這個代碼或許對你有所幫助:

UIImageView *cellImageView = [[UIImageView alloc] 
           initWithFrame:CGRectMake(0, 
                 0, 
                 cell.frame.size.width, 
                 cell.frame.size.height 
                 )]; 

    cellImageView.contentMode = UIViewContentModeScaleAspectFit; 
    // normal background view 
    [cellImageView setImage:[UIImage imageNamed:@"*<ImageName>*"]]; 

    [cell addSubview:cellImageView]; 
    [cell sendSubviewToBack:cellImageView]; 

    [cellImageView release], cellImageView = nil; 

這裏cell是定製UITableViewCell的對象。您也可以設置backgroundColor屬性。

+0

這段代碼不適用於你嗎?如果你發現任何人的答案是正確的,那麼PLZ標誌是被接受的。 – Mrunal

+0

我可以看到這將如何工作,但我還沒有嘗試過。有沒有辦法做到這一點,而不添加一個額外的子視圖到單元格? –

+0

這不起作用。我也已經設置了backgroundColor,這似乎也不起作用。 –

7

正如我們從伊娃爾selectedBackgroundView的名字可以看到的,這個背景在被選中時由單元格顯示。
我已經重新加載一些方法(- 的setSelected:動畫:- setHighlighted:動畫:)的UITableViewCell子類的重置子視圖的背景顏色改回自己的價值觀。看的喜歡的UIKit做這個模板方法的一些magic(遍歷所有的UIView子類和他們的背景設置爲clearColor

+0

這看起來是正確的。我設法通過使用UIImageView顯示圖像來顯示子視圖。該圖像屬性不會像背景顏色一樣清除。 – skorulis

+0

你救了我:) –

0

我會嘗試同時containerView阿爾法和子視圖設置爲1.0

[containerView setAlpha:1.0]; 
... 
[subview setAlpha:1.0]; 

這應該會讓你的控件完全不透明。

您也可以爲背景創建一些圖像,並在創建2個視圖的狀態下使用該圖像。假設您創建2個圖像(normalBackground.png和selectedBackground.png),然後將此圖像設置爲單元格背景。 Here是一個很好的教程。

+0

我也試過這個。它沒有工作。 –