IOS自定义UITableViewCell的高亮背景色

IOS的sdk中,对UITableViewCell的高亮背景色只支持两种颜色,分别为UITableViewCellSelectionStyleBlue和UITableViewCellSelectionStyleGray。
那么如何自定义这个颜色呢。一个思路是当用户点下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的高亮背景色。

 

文章来源:http://www.cnblogs.com/luanmage/p/4667264.html

posted @ 2015-07-22 14:30  滴水穿石_01  阅读(143)  评论(0编辑  收藏  举报