我想請(qǐng)問(wèn)你這么一個(gè)問(wèn)題?你在使用 UITableView 這個(gè)控件過(guò)程中,是不是會(huì)出現(xiàn)這么一種狀況,當(dāng) UITableViewCell 處于選中狀態(tài)的時(shí)候,UITableViewCell 里面的子控件背景顏色莫名其妙消失了 ?我就有遇到過(guò)這個(gè)問(wèn)題,現(xiàn)在我來(lái)給你講講我的問(wèn)題解決方法。如果你曾經(jīng)或者現(xiàn)在遇到過(guò)這個(gè)問(wèn)題并且也解決了,那么歡迎你在留言區(qū)分享你的解決方法,如果你還沒(méi)有解決這個(gè)問(wèn)題,那么歡迎你嘗試我的問(wèn)題解決方法。
Demo 時(shí)間
我們先上一個(gè) Demo ,在 Main.storyboard 里面 的 ViewController 里面添加一個(gè) TableView,并設(shè)置好 UITableViewDelegate 和UITableViewDataSource 。

再來(lái)新建一個(gè) TableViewCell, 這個(gè) Cell 叫做 DemoTableViewCell ,包含一個(gè)綠色背景的 Button,一個(gè)藍(lán)色背景的 Label,一個(gè)紅色背景的 View 。

接下來(lái)看看 ViewController 的代碼
#import "ViewController.h"
#import "DemoTableViewCell.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.tableView registerNib:[UINib nibWithNibName:@"DemoTableViewCell" bundle:nil] forCellReuseIdentifier:@"DemoTableViewCell"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
DemoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DemoTableViewCell"];
return cell;
}
@end
最后運(yùn)行 Demo ,查看運(yùn)行效果,效果正常。

好了,我們選中 Demo 界面中的一行,看看會(huì)出現(xiàn)什么。UITableViewCell 處于選中狀態(tài),UIView 的背景顏色消失。

第一個(gè)解決方案,在 ViewController.m 文件,我們實(shí)現(xiàn) UITableViewDelegate 的選擇方法,并調(diào)用 UITableViewDelegate 的取消選擇方法。
這個(gè)解決方案有一個(gè)缺陷不知道你發(fā)現(xiàn)沒(méi)有 ? 從選中狀態(tài)到取消選中狀態(tài)之間的時(shí)間段內(nèi),UIView 的背景顏色消失的問(wèn)題會(huì)出現(xiàn),不過(guò)有時(shí)候這個(gè)方案就能夠滿足我們的需求了。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
接下來(lái)看看第二個(gè)解決方案 ,打開(kāi)
DemoTableViewCell.m 文件,在 setSelected:animated: 方法里面重新設(shè)置 button,label,view 的背景顏色。
#import "DemoTableViewCell.h"
@implementation DemoTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
self.button.backgroundColor = [UIColor greenColor];
self.label.backgroundColor = [UIColor blueColor];
self.view.backgroundColor = [UIColor redColor];
}
@end
運(yùn)行 Demo 查看效果,你可以看到 UITableViewCell 處于選中狀態(tài),UIView 的背景顏色是正常的。

當(dāng)我們長(zhǎng)按 UITableViewCell 的時(shí)候,UIView 的背景顏色消失還是會(huì)出現(xiàn),那么我們就繼續(xù)在 setHighlighted: animated: 方法里面重新設(shè)置 button,label,view 的背景顏色。
#import "DemoTableViewCell.h"
@implementation DemoTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
[self restoreStatus];
}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
[super setHighlighted:highlighted animated:animated];
[self restoreStatus];
}
-(void)restoreStatus{
self.button.backgroundColor = [UIColor greenColor];
self.label.backgroundColor = [UIColor blueColor];
self.view.backgroundColor = [UIColor redColor];
}
@end
運(yùn)行 Demo 查看效果。這次不管我們是選中 還是 長(zhǎng)按 UITableViewCell 都不會(huì)出現(xiàn) UIView 的背景顏色消失的問(wèn)題,說(shuō)明我們的第二個(gè)解決方案是可行的,能夠完全處理掉這個(gè)問(wèn)題。

第三個(gè)解決方案,不過(guò)我不建議使用這個(gè)方案。這個(gè)方案是將 UITableViewCell 的選擇效果全部取消,也就意味著當(dāng)用戶點(diǎn)擊或者長(zhǎng)按 UITableViewCell 的時(shí)候都不會(huì)出現(xiàn)動(dòng)畫效果。這個(gè)解決方案不知道你能不能接受?反正我是接受不了。
#import "DemoTableViewCell.h"
@implementation DemoTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
self.selectionStyle = UITableViewCellSelectionStyleNone;
}
@end
總結(jié)
UITableViewCell 處于選中狀態(tài),UIView 的背景顏色消失,我猜測(cè)這個(gè)問(wèn)題起源于當(dāng) UITableViewCell 處于選中狀態(tài)也就是 selected ,UITableViewCell 的 View 也被置于 selected 。當(dāng) UITableViewCell 處于 highlighted,UITableViewCell 的 View 也被置于 highlighted。但是 UITableViewCell 的 setHighlighted: animated: 和 setSelected:animated: 并沒(méi)有實(shí)現(xiàn)對(duì) View 背景顏色的處理,所以出現(xiàn)了 view 背景顏色不對(duì)的問(wèn)題。具體的分析可以參考 stackoverflow 的 這個(gè)帖子 https://stackoverflow.com/questions/5222736/uiview-backgroundcolor-disappears-when-uitableviewcell-is-selected