为什么在 UITableViewCell select 上所有背景都消失了? [英] Why do all backgrounds disappear on UITableViewCell select?

查看:27
本文介绍了为什么在 UITableViewCell select 上所有背景都消失了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前项目的 UITableViewCell 行为让我感到困惑.我有一个相当简单的 UITableViewCell 子类.它向基本视图添加了一些额外的元素(通过 [self.contentView addSubview:...] 并在元素上设置背景颜色,使它们看起来像黑色和灰色的矩形框.

My current project's UITableViewCell behavior is baffling me. I have a fairly straightforward subclass of UITableViewCell. It adds a few extra elements to the base view (via [self.contentView addSubview:...] and sets background colors on the elements to have them look like black and grey rectangular boxes.

因为整个表格的背景都有这种类似混凝土的纹理图像,所以每个单元格的背景都需要是透明的,即使被选中,但在这种情况下,它应该变暗一点.我已经设置了自定义的半透明选定背景来实现此效果:

Because the background of the entire table has this concrete-like texture image, each cell's background needs to be transparent, even when selected, but in that case it should darken a bit. I've set a custom semi-transparent selected background to achieve this effect:

UIView *background = [[[UIView alloc] initWithFrame:self.bounds] autorelease];
background.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
background.opaque = NO;

[self setSelectedBackgroundView:background];

虽然这会产生正确的背景外观,但当我选择单元格时会发生奇怪的副作用;所有其他背景都以某种方式关闭关闭.这是屏幕截图.底部单元格看起来应该并且没有被选中.顶部单元格被选中,但它应该显示黑色和灰色矩形区域,但它们不见了!

And although that yields the right look for the background, a weird side effect happens when I select the cell; all other backgrounds are somehow turnt off. Here's a screenshot. The bottom cell looks like it should and is not selected. The top cell is selected, but it should display the black and grey rectangular areas, yet they are gone!

谁知道这里发生了什么,更重要的是:我该如何纠正?

Who knows what's going on here and even more important: how can I correct this?

推荐答案

发生的事情是 TableViewCell 内的每个子视图都将接收 setSelected 和 setHighlighted 方法.setSelected 方法将删除背景颜色,但如果您将其设置为选定状态,它将被更正.

What is happening is that each subview inside the TableViewCell will receive the setSelected and setHighlighted methods. The setSelected method will remove background colors but if you set it for the selected state it will be corrected.

例如,如果那些是作为子视图添加到自定义单元格中的 UILabels,那么您可以将其添加到 TableViewCell 实现代码的 setSelected 方法中:

For example if those are UILabels added as subviews in your customized cell, then you can add this to the setSelected method of your TableViewCell implementation code:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    self.textLabel.backgroundColor = [UIColor blackColor];

}

其中 self.textLabel 将是上图中显示的标签

where self.textLabel would be whatever those labels are that are shown in the picture above

我不确定你在哪里添加你选择的视图,我通常在 setSelected 方法中添加它.

I'm not sure where your adding your selected view, I usually add it in the setSelected method.

或者,您可以子类化 UILabel 并覆盖 setHighlighted 方法,如下所示:

Alternatively, you can subclass the UILabel and override the setHighlighted method like so:

-(void)setHighlighted:(BOOL)highlighted
{
    [self setBackgroundColor:[UIColor blackColor]];
}

这篇关于为什么在 UITableViewCell select 上所有背景都消失了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆