簡體   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