如题,实现此效果需要重写cell的点击和高亮方法来保持子控件的背景颜色
代码如下:
colorViews:记录需要保持背景颜色的views,可在子控件初始化后添加
NSArray *colorViews; ///< cell高亮或点击状态时需要保持背景颜色的views
- (void)setSelected:(BOOL)selected animated:(BOOL)animated{
if (self.colorViews && self.colorViews.count > 0) {
NSMutableArray *colors = [NSMutableArray array];
NSMutableArray *views = [NSMutableArray array];
for (UIView *view in self.colorViews) {
if (view.backgroundColor && ![view.backgroundColor isEqual:[UIColor clearColor]]) {
[colors addObject:view.backgroundColor];
[views addObject:view];
}
}
[super setSelected:selected animated:animated];
for (NSInteger i = 0; i < colors.count; i ++) {
UIView *view = [views objectAtIndex:i];
UIColor *color = [colors objectAtIndex:i];
[view setBackgroundColor:color];
}
}else{
[super setSelected:selected animated:animated];
}
}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
if (self.colorViews && self.colorViews.count > 0) {
NSMutableArray *colors = [NSMutableArray array];
NSMutableArray *views = [NSMutableArray array];
for (UIView *view in self.colorViews) {
if (view.backgroundColor && ![view.backgroundColor isEqual:[UIColor clearColor]]) {
[colors addObject:view.backgroundColor];
[views addObject:view];
}
}
[super setHighlighted:highlighted animated:animated];
for (NSInteger i = 0; i < colors.count; i ++) {
UIView *view = [views objectAtIndex:i];
UIColor *color = [colors objectAtIndex:i];
[view setBackgroundColor:color];
}
}else{
[super setHighlighted:highlighted animated:animated];
}
}