我通过以下方式将彩色状态指示器附加到每个 UITableViewCell 的左侧:
CGRect rect = CGRectMake(3, 1, 12, 42);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
switch (cellState)
{
case State_OK:
CGContextSetFillColorWithColor(context,
[[UIColor customGreen] CGColor]);
break;
case State_Warning:
CGContextSetFillColorWithColor(context,
[[UIColor customYellow] CGColor]);
break;
case State_Critical:
CGContextSetFillColorWithColor(context,
[[UIColor customRed] CGColor]);
break;
case State_Unknown:
CGContextSetFillColorWithColor(context,
[[UIColor customGray] CGColor]);
break;
}
CGContextFillRect(context, rect);
UIImageView *imageView = [[UIImageView alloc] initWithImage:UIGraphicsGetImageFromCurrentImageContext()];
imageView.alpha = 0.7;
UIGraphicsEndImageContext();
[cell.contentView addSubview:imageView];
return cell;
但不知何故我得到了非常奇怪的颜色:
当我设置 imageView.alpha = 1;
时,颜色又变好了:
像这样添加图像:
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
cell.imageView.alpha = 0.7;
工作得很好!即使是 alpha
。但我真的不想那样做。
最佳答案
问题可能是颜色与单元格的背景混合在一起。单元格的背景颜色是什么?
另一个原因可能是您的细胞图像被重复使用,因此您将图像设置在之前图像的顶部。
关于ios - 将彩色 UIImageView 添加到 UITableViewCell 时的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19904252/