0

我正在使用此代码自定义单元格背景视图的颜色:

UIColor *cellBackgroundColor = [UIColor colorWithRed:255.0 green:255.0 blue:255.0 alpha:1.0];
cell.contentView.backgroundColor = cellBackgroundColor;

问题是,它不起作用。不过,这段代码工作得很好:

cell.contentView.backgroundColor = [UIColor redColor];

这里有什么问题?我不能根据自己的喜好自定义单元格的颜色吗?还是我做错了什么?

4

5 回答 5

2

只是改变 :

UIColor *cellBackgroundColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0];
cell.contentView.backgroundColor = cellBackgroundColor;
于 2013-01-22T08:35:14.403 回答
2

您必须使用此委托设置单元格背景:-

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

   // UIImage *img = [UIImage imageNamed:@"button.png"];  // get your background image

    UIColor *cellBackgroundColor = [UIColor colorWithRed:255/255.0 green:108/255.0 blue:61/255.0 alpha:1.0]; //if you want to set color then use this line

  //  UIColor *backgroundColor = [[UIColor alloc] initWithPatternImage: img];
    cell.backgroundColor = cellBackgroundColor;
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.detailTextLabel.backgroundColor = [UIColor clearColor];
}

你的桌子看起来像: -

在此处输入图像描述

获取 RGB 颜色并在 iPhone 上使用 UIColor 对其进行标准化

Your values are between 0 and 255. Use them to create a UIColor:

    float r; float g; float b; float a;

    [UIColor colorWithRed:r/255.f
                    green:g/255.f
                     blue:b/255.f    
                    alpha:a/255.f];
于 2013-01-22T08:36:13.157 回答
1

也许您可以cell.contentView.backgroundColor = [UIColor clearColor]在设置颜色之前添加。

原因是背景视图在底部。内容视图位于顶部。

其他逻辑是

UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];

myBackView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:0.75 alpha:1];

[cell.contentView addSubview:myBackView];

[myBackView release];
于 2013-01-22T08:40:34.873 回答
1
cell.backgroundView.backgroundColor

您可以制作自定义视图并可以更改完整的背景视图

UIView *backgroundViewForCell = [[[UIView alloc] init] autorelease];
backgroundViewForCell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"row-bg.png"]];
UIView *selectedBackgroundViewForCell = [[[UIView alloc] init] autorelease];
selectedBackgroundViewForCell.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:.2];
cell.backgroundView = backgroundViewForCell;
cell.selectedBackgroundView = selectedBackgroundViewForCell;
于 2013-01-22T08:35:45.650 回答
0

你在正确的轨道上。你只需要用这个替换你的颜色代码

UIColor *cellBackgroundColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0];

享受编程!!

于 2013-01-22T08:38:50.387 回答