UITableview作為最常見的控件之一,使用的花樣越來越多,頁面也越來越多樣化,所以,系統(tǒng)提供的cell的樣式已不能滿足我們開發(fā)者的需求,此時就需要自定義控件,有的時候,針對這些自定義cell的某個控件(比如UILabel),會加上背景顏色的設(shè)置,但是在點擊cell的時候,這個背景顏色會消失,原因是:在點擊cell的時候這個控件的高亮狀態(tài)未設(shè)置顏色!!!

長按cell后中間兩條線的顏色會消失,變成透明色的,就不上圖了.
此時有兩種方法解決,第一種,在代理方法中控制UITableViewCell的點選狀態(tài),為UITableViewCellSelectionStyleNone。
代碼:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
? ? ? ? [tableView deselectRowAtIndexPath:indexPath animated:YES];
? ? ? ? ZPOrderCell *cell = [tableView cellForRowAtIndexPath:indexPath];
? ? ? ? cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
但是這樣cell就沒有點選狀態(tài)了,其實就相當(dāng)于一次性消費的感覺.
.如果需要有UITableViewCell的點選狀態(tài),但是不希望在點選以后其中控件的顏色并不變成透明色,那么方法為:
//這個方法在用戶按住Cell時被調(diào)用
-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
? ? ? ? [super setHighlighted:highlighted animated:animated];
? ? ? ? self.preLineLab.backgroundColor = [UIColor zp_colorWithHex:0xeeeee];
? ? ? ? self.nextLineLab.backgroundColor = [UIColor zp_colorWithHex:0xeeeee];
}
//這個方法是cell被選中或取消時調(diào)用
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
? ? ? ?[super setSelected:selected animated:animated];
? ? ? ?self.preLineLab.backgroundColor = [UIColor zp_colorWithHex:0xeeeee];
? ? ? ?self.nextLineLab.backgroundColor = [UIColor zp_colorWithHex:0xeeeee];
}
上邊的[UIColor zp_colorWithHex:0xeeeee]是樓主自定義的UIColor的分類方法,用來傳入一個十六進制的數(shù)據(jù)生成相應(yīng)的顏色.跟本文章內(nèi)容無關(guān).