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

BrF*_*eek 4 objective-c uitableview ios ios7

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

解:

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

Sul*_*han 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
Run Code Online (Sandbox Code Playgroud)

将其添加到您的单元格(可能不是contentView直接添加到单元格)并将其框架设置为与单元格的边界匹配.