Question

I am attaching a colorful state indicator to the left of every UITableViewCell the following way:

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:

enter image description here

When I set imageView.alpha = 1; The colors are fine again:

enter image description here

Adding the Images like this:

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

Works just fine! Even with alpha. But I don't really want to do it that way.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top