简体   繁体   English

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

[英]Strange behavior when adding colorful UIImageView to a UITableViewCell

I am attaching a colorful state indicator to the left of every UITableViewCell the following way: 我通过以下方式将彩色状态指示器附加到每个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;

But somehow I am getting very strange colors: 但是不知何故我得到了非常奇怪的颜色:

在此处输入图片说明

When I set imageView.alpha = 1; 当我设置imageView.alpha = 1; The colors are fine again: 颜色再次很好:

在此处输入图片说明

Adding the Images like this: 像这样添加图像:

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

Works just fine! 效果很好! Even with alpha . 即使使用alpha But I don't really want to do it that way. 但是我真的不想那样做。

The problem could be that the colours are blending with the background of the cell. 问题可能在于颜色正在与单元格背景混合​​。 What is the background color of the cell? 单元的背景颜色是什么?

Another reason could be that your cells images are being reused, so you are setting the images on top previous images. 另一个原因可能是您的单元格图像正在被重用,因此您将图像设置在先前图像的顶部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM