繁体   English   中英

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

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

我通过以下方式将彩色状态指示器附加到每个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 但是我真的不想那样做。

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

另一个原因可能是您的单元格图像正在被重用,因此您将图像设置在先前图像的顶部。

暂无
暂无

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

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