我正在使用此代码来自定义单元格背景视图的颜色:
UIColor *cellBackgroundColor = [UIColor colorWithRed:255.0 green:255.0 blue:255.0 alpha:1.0];
cell.contentView.backgroundColor = cellBackgroundColor;
事情是,它不起作用。但是这段代码非常合适:
cell.contentView.backgroundColor = [UIColor redColor];
这里有什么问题?我不能根据自己的喜好自定义单元格的颜色吗?或者我做错了什么?
答案 0 :(得分: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;
答案 1 :(得分: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];
}
你的表格如下: -
Taking an RGB color and normalizing it with UIColor on the iPhone
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];
答案 2 :(得分: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;
答案 3 :(得分:1)
也许您可以在设置颜色之前添加cell.contentView.backgroundColor = [UIColor clearColor]
。
原因是backgroundview位于底部。内容视图位于顶部。
其他LOgic IS
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];
答案 4 :(得分:0)
你走在正确的轨道上。您只需要用此
替换您的颜色代码UIColor *cellBackgroundColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0];
享受编程!!