为什么所有的背景消失在UITableViewCell选择? [英] Why do all backgrounds disappear on UITableViewCell select?

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

问题描述

我当前项目的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!

谁知道这里发生了什么

推荐答案

发生的是,TableViewCell中的每个子视图都将接收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选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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