ios - 将彩色 UIImageView 添加到 UITableViewCell 时的奇怪行为

标签 ios uitableview uiimageview uiimage alpha

我通过以下方式将彩色状态指示器附加到每个 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;

但不知何故我得到了非常奇怪的颜色:

enter image description here

当我设置 imageView.alpha = 1; 时,颜色又变好了:

enter image description here

像这样添加图像:

cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
cell.imageView.alpha = 0.7;

工作得很好!即使是 alpha。但我真的不想那样做。

最佳答案

问题可能是颜色与单元格的背景混合在一起。单元格的背景颜色是什么?

另一个原因可能是您的细胞图像被重复使用,因此您将图像设置在之前图像的顶部。

关于ios - 将彩色 UIImageView 添加到 UITableViewCell 时的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19904252/

相关文章:

ios - UIView 内的 UITableView - 仅当在 uitableview 外滚动单元格时才加载数据

ios - 如何在没有 UITableView 的情况下将自定义 UITableViewCell 与 xib 一起使用

iOS 使用 Segue 导航到表格单元格上的新 View 单击

iphone - cellForRowAtIndexPath : not called

ios - 通过索引路径更改 CLLocationCooperative2DMake 使用数组

ios - 如何按名称检索 Sprite 的位置

ios - SWIFT 删除 subview

ios - UIimageView 蒙版成菱形

ios - 自定义 UITableViewCell 图像在滚动之前不会显示

ios - prepareForSegue :sender: is never called