如題,實(shí)現(xiàn)此效果需要重寫cell的點(diǎn)擊和高亮方法來保持子控件的背景顏色
代碼如下:
colorViews:記錄需要保持背景顏色的views,可在子控件初始化后添加
NSArray *colorViews; ///< cell高亮或點(diǎn)擊狀態(tài)時(shí)需要保持背景顏色的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];
}
}