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的高亮背景色。
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated