2 for 2012 for_2012 于 2013.01.23 16:08 提问

无法设置UITableViewCell的背景颜色

我用下面的代码自定义背景颜色:

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

但是没有出现任何结果。这行代码就运行正常:

cell.contentView.backgroundColor = [UIColor redColor];

这是什么问题?请帮忙解答一下谢谢。

2个回答

Chengzi_963
Chengzi_963   2013.01.23 16:43
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;
prettYYoyic
prettYYoyic   2013.01.23 16:59

用delegates设置背景:

- (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];
}

然后出现的效果应该是这样:

CSDN移动问答

选择一个颜色,设置为iphone中的UI颜色

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];
tbfyniiswcy
tbfyniiswcy 正解
大约 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!