UITableViewCell reorders the cleansing color of the background image

This question has been asked several times, but there seems to be no solution for this.

UITableView rewrites hidden background

UITableViewCell subview not showing during reordering

Reordering causing subview highlight color to clear

I have a custom tableview cell with UILabel. When the tableview is in edit mode and I drag the cell to reorder, the UILabel background becomes transparent. I also found that if I try to reorder the selected cell (in my view mode multiple selection is allowed in edit mode), the background color of the subheading will remain.

I tried using the methods in my CustomCell, but none of them overrides the background color of the subtitle when dragging a cell.

I want the background color in subview to remain. Is there any method that I skipped? Or did Apple design it that way?

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    if (selected) {
        if (self.isEditing) {
            self.customLabel.backgroundColor = [UIColor blueColor];
        }
        else {
            self.customLabel.backgroundColor = [UIColor blueColor];
        }
    }
    else {
            self.customLabel.backgroundColor = [UIColor blueColor];
    }
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    [super setHighlighted:highlighted animated:animated];

    if (highlighted) {
        if (self.isEditing) {
            self.customLabel.backgroundColor = [UIColor blueColor];
        }
        else {
            self.customLabel.backgroundColor = [UIColor blueColor];
        }
    }
    else {
        self.customLabel.backgroundColor = [UIColor blueColor];
    }
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];

    if (editing) {
        self.customLabel.backgroundColor = [UIColor blueColor];
    }
    else {
        self.customLabel.backgroundColor = [UIColor blueColor];
    }
}
+4
source share
2 answers

You can create your own custom UILabel. And overload is -drawRect.

@interface VALAbel:UILabel
@property (nonatomic, strong) UIColor *bac_color;
@end


@implementation VALabel

-(void)setBac_color:(UIColor *)bac_color
{
_bac_color = bac_color;
[self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
   [self.bac_color set];

   CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), true);
   CGContextFillRect(UIGraphicsGetCurrentContext(), self.bounds);

    [super drawRect:rect];
 }
@end

It will help you!

+1
source

Adding a modern solution for Swift 3.

Create a new .swift file and create a custom UIView class:

import UIKit

class NeverClearView: UIView {
    override var backgroundColor: UIColor? {
        didSet {
            if backgroundColor?.cgColor.alpha == 0 {
                backgroundColor = oldValue
            }
        }
    }
}

In Interface Builder, go to the Identity Inspector and set the class to your new NeverClearView class. This will stop it from disappearing when reordering a cell.

UIKit, , UIStepper .

+2

Source: https://habr.com/ru/post/1585045/


All Articles