我正在开发一个包含 UITableView 的应用程序。用户唯一需要做的事情就是重新排序 UITableView 的内容。在编辑模式下,tableview 单元格的背景颜色变为透明。有什么方法可以在重新排序时保持 tableview 单元格的原始背景颜色?
解决方案:
经过一些额外的搜索,我找到了(简单的)解决方案。 通过在willDisplayCell方法中添加cell.backgroundView.backgroundcolor,问题得到解决。
最佳答案
iOS UI 框架会使您的单元格透明的情况有很多种。其中之一是高亮/选中状态,另一个是重新排序情况。
重要的是了解系统是如何工作的。系统隐藏单元格背景(cell.backgroundView
或 cell.selectedBackgroundView
),并且对于单元格中的每个 View ,它都将 backgroundColor
设置为透明色。因此,不可能使用 backgroundColor
来保持单元格不透明。
最简单的解决方案是添加一个带有 drawRect:
的简单 UIView
,它将用一种颜色填充单元格。
@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
,而是直接添加到单元格)并设置其框架以匹配单元格的边界。
关于ios - 在编辑模式下重新排序时更改 UITableViewCell 的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21040730/