[IOS 开发] 自定义(重写) UITableViewCell的高亮背景色

时间:2015-06-05 19:10:14   收藏:0   阅读:303

IOS的sdk中,对UITableViewCell的高亮背景色只支持两种颜色,分别为UITableViewCellSelectionStyleBlueUITableViewCellSelectionStyleGray

那么如何自定义这个颜色呢。一个思路是当用户点下cell时设置你想要的cell的背景色,当释放点击时给cell重新设回原来的背景色,这样就能达到预想的效果了。

下面是具体实现的代码:

 

 

- (void)drawRect:(CGRect)rect  
{  
    if (self.highlighted) {  
        self.backgroundColor = [UIColor colorWithHexString:@"0x383838"];  
    }else{  
        self.backgroundColor = [UIColor clearColor];  
    }  
}  
  
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated  
{  
    [super setHighlighted:highlighted animated:animated];  
    [self setNeedsDisplay];  
}  

 

1. 继承UITableViewCell

        2. 重写以上两个方法。

         当每次用户点击或者释放的时候,系统都会来调用下面这个方法,从而来改变cell的高亮背景色。

 

 

 

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated

 

原文:http://www.cnblogs.com/missajj/p/4555231.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!