我正在使用此代码自定义单元格背景 View 的颜色:
UIColor *cellBackgroundColor = [UIColor colorWithRed:255.0 green:255.0 blue:255.0 alpha:1.0];
cell.contentView.backgroundColor = cellBackgroundColor;
问题是,它不起作用。这段代码虽然工作得很好:
cell.contentView.backgroundColor = [UIColor redColor];
这里有什么问题?我不能根据自己的喜好自定义单元格的颜色吗?还是我做错了什么?
最佳答案
你必须用这个 Delegate 设置 Cell Background:-
- (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];
}
你的 table 看起来像:-
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];
关于iphone - 无法在 UITableViewCell 中设置自定义背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14454406/