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

时间:2014-01-10 09:39:26

标签: ios objective-c uitableview ios7

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

解决方案:

经过几次额外搜索后,我找到了(简单)解决方案。 通过将cell.backgroundView.backgroundcolor添加到willDisplayCell方法,问题得以解决。

2 个答案:

答案 0 :(得分:6)

iOS UI框架将使您的单元格透明,有各种各样的场景。其中一个是突出显示/选定状态,另一个是重新排序的情况。

重要的是要了解系统是如何做到的。系统会隐藏单元格背景(cell.backgroundViewcell.selectedBackgroundView),并且对于单元格中的每个视图,它将backgroundColor设置为透明色。因此,无法使用backgroundColor来保持单元格不透明。

最简单的解决方案是添加一个简单的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

将其添加到您的单元格(可能不是{{1}},而是直接添加到单元格)并设置其框架以匹配单元格的边界。

答案 1 :(得分:-1)

说你的细胞颜色是红色

 [cell setBackgroundColor:[UIColor redColor]];

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

 tableView.backgroundColor = [UIColor redColor];
相关问题