2010-02-18 87 views
1

我有一個UITableViewStyleGroup風格的UITableViewCell,我想改變單元格的背景顏色。UITableViewCell顯示不正確的基於alpha的背景顏色

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)ip { 
    // Do cell creation stuff ... 

    cell.backgroundColor = 
     [UIColor colorWithRed:255.0/255.0 green:243.0/255.0 blue:175.0/255.0 alpha:0.50]; 
} 

問題是,這不能在網格上正確顯示;我在UITableViewStylePlain上使用相同的顏色,並且顯示正確。我正在開發OS 3.0,並已閱讀設置背景色的multipleposts。我可以設置它只是沒有正確設置的顏色!我錯過了什麼?

Incorrect yellow background with Alpha Correct yellow background with Alpha

+0

的UITableViewCell沒有backgroundColor屬性....莫非是你的問題? – 2010-03-04 23:02:41

+0

@Chip它呢,'backgroundColor'是從'UIView'繼承的 – 2010-03-04 23:25:24

+0

當然可以。抱歉。這是一個自定義的子類還是一個預定義的樣式? – 2010-03-05 13:30:09

回答

1

你必須做你的細胞創建/複用邏輯的東西來改變默認的行爲。從頭開始一個項目,並實施此代碼的工作對我來說:

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // Configure the cell. 
    cell.backgroundColor = [UIColor colorWithRed:1.0 green:0 blue:0 alpha:1.0]; 

    return cell; 
} 

仔細檢查documentation如果你需要更復雜的東西。

另外,由於兩種不同風格的表背景顏色不同,顏色是否會發生變化? UITableViewStylePlain tableView具有默認的白色背景。 UITableViewStyleGrouped tableView將具有灰色背景。由於您將alpha設置爲0.5,因此它會覆蓋到兩個不同的顏色背景上,併爲您提供顏色偏移。

+0

@Chip - 謝謝你的回答,我會試一試看看會發生什麼。我想你可能是對灰色背景顏色疊加的。如果這是問題,那有沒有解決方案? – 2010-03-05 19:41:57

+0

如果這是問題,請繼承UITableViewCell。然後,您可以將「純色背景單元格」設爲白色,並在其前面添加帶有alpha的彩色背景。或者,如果透明度不是真的需要,找出單元格的不透明顏色並設置它。 – 2010-03-05 22:19:40

0

我確定這種方法不被支持,但它確實有效。關閉xcode,在文本編輯器(如vi)中打開.xib或.storyboard文件。找到您的表的XML並更改單元格顏色。例如,以下是默認白色表格單元格的原始部分:

<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" indicatorStyle="black" dataMode="prototypes" style="plain" rowHeight="44" sectionHeaderHeight="22" sectionFooterHeight="22" id="k63-au-YAF"> 
    <rect key="frame" x="0.0" y="0.0" width="320" height="480"/> 
    <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> 
    <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/> 
    <prototypes> 

查找並更改顏色標記。下面是一個包含原始帖子顏色的示例:

<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" indicatorStyle="black" dataMode="prototypes" style="plain" rowHeight="44" sectionHeaderHeight="22" sectionFooterHeight="22" id="k63-au-YAF"> 
    <rect key="frame" x="0.0" y="0.0" width="320" height="480"/> 
    <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> 
    <color key="backgroundColor" red="1.0" green="0.95294117647059" blue="0.68627450980392" alpha="0.5" colorSpace="calibratedRGB"/> 
    <prototypes> 

再次打開xcode,並且您的單元格顏色已更新。

注意:對於顏色代碼,255分之243= 0.95294117647059(綠色),255分之175= 0.68627450980392(紅色)等