UITableViewCell 处于选中状态,UIView 的背景颜色消失

我想请问你这么一个问题?你在使用 UITableView 这个控件过程中,是不是会出现这么一种状况,当 UITableViewCell 处于选中状态的时候,UITableViewCell 里面的子控件背景颜色莫名其妙消失了 ?我就有遇到过这个问题,现在我来给你讲讲我的问题解决方法。如果你曾经或者现在遇到过这个问题并且也解决了,那么欢迎你在留言区分享你的解决方法,如果你还没有解决这个问题,那么欢迎你尝试我的问题解决方法。

Demo 时间

我们先上一个 Demo ,在 Main.storyboard 里面 的 ViewController 里面添加一个 TableView,并设置好 UITableViewDelegate 和UITableViewDataSource 。


TableView

再来新建一个 TableViewCell, 这个 Cell 叫做 DemoTableViewCell ,包含一个绿色背景的 Button,一个蓝色背景的 Label,一个红色背景的 View 。


image.png

接下来看看 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

最后运行 Demo ,查看运行效果,效果正常。


运行效果

好了,我们选中 Demo 界面中的一行,看看会出现什么。UITableViewCell 处于选中状态,UIView 的背景颜色消失。

选中 Demo 界面中的一行

第一个解决方案,在 ViewController.m 文件,我们实现 UITableViewDelegate 的选择方法,并调用 UITableViewDelegate 的取消选择方法。

这个解决方案有一个缺陷不知道你发现没有 ? 从选中状态到取消选中状态之间的时间段内,UIView 的背景颜色消失的问题会出现,不过有时候这个方案就能够满足我们的需求了。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

接下来看看第二个解决方案 ,打开
DemoTableViewCell.m 文件,在 setSelected:animated: 方法里面重新设置 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

运行 Demo 查看效果,你可以看到 UITableViewCell 处于选中状态,UIView 的背景颜色是正常的。

背景正常

当我们长按 UITableViewCell 的时候,UIView 的背景颜色消失还是会出现,那么我们就继续在 setHighlighted: animated: 方法里面重新设置 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

运行 Demo 查看效果。这次不管我们是选中 还是 长按 UITableViewCell 都不会出现 UIView 的背景颜色消失的问题,说明我们的第二个解决方案是可行的,能够完全处理掉这个问题。

image.png

第三个解决方案,不过我不建议使用这个方案。这个方案是将 UITableViewCell 的选择效果全部取消,也就意味着当用户点击或者长按 UITableViewCell 的时候都不会出现动画效果。这个解决方案不知道你能不能接受?反正我是接受不了。

#import "DemoTableViewCell.h"
@implementation DemoTableViewCell
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}
@end

总结

UITableViewCell 处于选中状态,UIView 的背景颜色消失,我猜测这个问题起源于当 UITableViewCell 处于选中状态也就是 selected ,UITableViewCell 的 View 也被置于 selected 。当 UITableViewCell 处于 highlighted,UITableViewCell 的 View 也被置于 highlighted。但是 UITableViewCell 的 setHighlighted: animated: 和 setSelected:animated: 并没有实现对 View 背景颜色的处理,所以出现了 view 背景颜色不对的问题。具体的分析可以参考 stackoverflow 的 这个帖子 https://stackoverflow.com/questions/5222736/uiview-backgroundcolor-disappears-when-uitableviewcell-is-selected

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容