Reputation: 240
I'm working on a application containing a UITableView. The only thing which needs to be possible for users, is to reorder the contents of the UITableView. When in editing mode the background color of the tableview cells turns transparent. Is there any way of keeping the original background color of the tableview cell when it's being reordered?
Solution:
After a few extra searches I found the (simple) solution. By adding cell.backgroundView.backgroundcolor to the willDisplayCell method, the problem was solved.
Upvotes: 4
Views: 3059
Reputation: 130172
There are various scenarios when the iOS UI framework will make your cell transparent. One of them is the highlighted/selected state and another one is the reordering case.
The important thing is to understand how the system does it. The system hides the cell background (cell.backgroundView
or cell.selectedBackgroundView
) and for every view in the cell it sets backgroundColor
to transparent color. Therefore, it's not possible to use backgroundColor
to keep the cell opaque.
The simplest solution is to add a simple UIView
with drawRect:
that will fill the cell with a color.
@interface MyColoredView : UIView
@property (nonatomic, strong, readwrite) UIColor *color;
@end
@implementation MyColoredView
- (void)setColor:(UIColor *)color {
_color = color;
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
[self.color set];
CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), true);
CGContextFillRect(UIGraphicsGetCurrentContext(), self.bounds);
}
@end
Add it to your cell (probably not to contentView
but directly to the cell) and set its frame to match the bounds of the cell.
Upvotes: 6
Reputation: 59
Say your cell color is redcolor
[cell setBackgroundColor:[UIColor redColor]];
then set the tableview background to same.
tableView.backgroundColor = [UIColor redColor];
Upvotes: -1