美文网首页
UITableViewCell点击或常亮时保持子控件的背景颜色

UITableViewCell点击或常亮时保持子控件的背景颜色

作者: iOSCoder_XH | 来源:发表于2018-10-27 10:48 被阅读16次

    如题,实现此效果需要重写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];
        }
    }
    

    相关文章

      网友评论

          本文标题:UITableViewCell点击或常亮时保持子控件的背景颜色

          本文链接:https://www.haomeiwen.com/subject/njartqtx.html