UITableViewCell選中后子View背景色改變

自定義了UITableViewCell,設置自己添加的label的backgroundColor為紫色,但是當Cell被選中的時候,lable的backgroundColor被改變了。觀察一下label的backgroundColor在cell選中時的變化。

 [RACObserve(self.lblSimple, backgroundColor) subscribeNext:^(id x) {
   
    NSLog(@"%@",x);
}];

輸出結果為

UIDeviceWhiteColorSpace 0 0
UIDeviceRGBColorSpace 0.541 0.761 0.953 1
UIDeviceRGBColorSpace 0.541 0.761 0.953 1
UIDeviceWhiteColorSpace 0 0
UIDeviceRGBColorSpace 0.541 0.761 0.953 1     
UIDeviceRGBColorSpace 0.541 0.761 0.953 1

這和cell的

- (void)setSelected:(BOOL)selected animated:(BOOL)animated;
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated ;   

密切相關。看看文檔,下面是

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

方法的文檔

Sets the highlighted state of the cell, optionally animating the transition 
between states.
    Highlights or unhighlights the cell, animating the transition between 
regular and highlighted state if animated is YES. Highlighting affects the 
appearance of the cell’s labels, image, and background.
    Note that for highlighting to work properly, you must fetch the cell’s 
label (or labels) using the textLabel (and detailTextLabel properties and set 
the label’s highlightedTextColor property; for images, get the cell’s image
 using the imageView property and set the UIImageView object’s
 highlightedImage property.
    A custom table cell may override this method to make any transitory 
appearance changes.

注意

Highlighting affects the appearance of the cell’s labels, image, and background.

另外一個方法

- (void)setSelected:(BOOL)selected animated:(BOOL)animated;

也差不多。
但個人感覺選中時cell的表現還和tableView的allowsSelection,allowsMultipleSelection,allowsSelectionDuringEditing,allowsMultipleSelectionDuringEditing,editting等屬性相關,cell自身的selectionStyle的影響自是不必說。具體測試代碼,有時間再補上。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容