2010-02-18 85 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(红色)等