在编辑模式下重新sorting时更改UITableViewCell的背景颜色

我正在处理包含UITableView的应用程序。 对用户来说唯一需要的是对UITableView的内容进行重新sorting。 在编辑模式下,tableview单元格的背景颜色变为透明。 有没有办法保持tableview单元格的原始背景颜色重新sorting时?

解:

经过一些额外的search后,我find了(简单的)解决scheme。 通过将cell.backgroundView.backgroundcolor添加到willDisplayCell方法,问题解决了。

iOS UI框架将使您的单元变得透明。 其中之一是突出显示/select状态,另一个是重新sorting的情况。

重要的是要了解系统是如何工作的。 系统隐藏单元格背景( cell.backgroundViewcell.selectedBackgroundView ),并为单元格中的每个视图设置backgroundColor为透明色。 因此,不可能使用backgroundColor来保持单元格不透明。

最简单的解决scheme是用drawRect:添加一个简单的UIView drawRect:它将用一种颜色填充单元格。

 @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 

将它添加到你的单元格(可能不是contentView但直接到单元格),并设置其框架以匹配单元格的边界。

说你的细胞颜色是红色的

  [cell setBackgroundColor:[UIColor redColor]]; 

然后将tableview背景设置为相同。

  tableView.backgroundColor = [UIColor redColor];