Change background color of UITableViewCell when reordering in editing mode

不打扰是莪最后的温柔 提交于 2020-01-11 09:17:30

问题


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.


回答1:


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.




回答2:


Say your cell color is redcolor

 [cell setBackgroundColor:[UIColor redColor]];

then set the tableview background to same.

 tableView.backgroundColor = [UIColor redColor];


来源:https://stackoverflow.com/questions/21040730/change-background-color-of-uitableviewcell-when-reordering-in-editing-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!