在编辑模式下重新排序时更改UITableViewCell的背景颜色 [英] Change background color of UITableViewCell when reordering in editing mode
问题描述
我正在处理包含UITableView的应用程序。用户唯一需要的是重新排序UITableView的内容。在编辑模式下,tableview单元格的背景颜色变为透明。在重新排序时,有没有办法保持tableview单元格的原始背景颜色?
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?
解决方案:
经过几次额外搜索后,我找到了(简单)解决方案。
通过将cell.backgroundView.backgroundcolor添加到willDisplayCell方法,问题得以解决。
After a few extra searches I found the (simple) solution. By adding cell.backgroundView.backgroundcolor to the willDisplayCell method, the problem was solved.
推荐答案
有各种场景当iOS UI框架将使您的单元格透明。其中一个是突出显示/选择状态,另一个是重新排序的情况。
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.
重要的是了解系统如何做到这一点。系统隐藏单元格背景( cell.backgroundView
或 cell.selectedBackgroundView
),并为其设置的单元格中的每个视图 backgroundColor
为透明色。因此,不可能使用 backgroundColor
来保持单元格不透明。
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.
最简单的解决方案是添加一个简单的 UIView
带 drawRect:
,它将用一种颜色填充单元格。
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
将其添加到您的手机中(可能不是 contentView
,而是直接到单元格)并设置其框架以匹配单元格的边界。
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.
这篇关于在编辑模式下重新排序时更改UITableViewCell的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!