不让cell的背景色跟着改变
时间: 2023-12-05 12:04:47 浏览: 30
当 UITableView 被选中时,会默认高亮选中的 UITableViewCell,可以通过 setHighlighted:animated: 方法来取消高亮选中的 UITableViewCell。
在 UITableViewCell 的子类中重写 setSelected:animated: 方法和 setHighlighted:animated: 方法,避免选中后导致 cell 的背景色跟着改变:
```objc
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
if (selected) {
self.backgroundColor = [UIColor clearColor];
}
}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[super setHighlighted:highlighted animated:animated];
if (highlighted) {
self.backgroundColor = [UIColor clearColor];
}
}
```
在上述代码中,当 UITableViewCell 被选中或高亮时,将其背景色设置为 [UIColor clearColor],即透明色,这样就能保证 cell 的背景色不会跟着改变了。