selectedBackgroundView修改contentView子视图 [英] The selectedBackgroundView modifies the contentView subviews

查看:110
本文介绍了selectedBackgroundView修改contentView子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自定义 backgroundView selectedBackgroundView 获取 UITableViewCell 子类。这些单元格位于分组表中,因此我根据 cellForRowAtIndexPath中的单元格行将背景和所选背景设置为 UIImageView

I'm using a custom backgroundView and selectedBackgroundView for a UITableViewCell subclass. These cells are in a grouped table, so I'm setting the background and selected background as UIImageViews based on the cell's row in cellForRowAtIndexPath:.

我遇到的问题是,当选择单元格时,其 selectedBackgroundView 修改 contentView 的内容。例如,在选择和/或突出显示单元格后, contentView 中的 UILabel 具有 backgroundColor 更改并且用作单元格分隔符的 UIView 不可见。

The problem I'm having is that when the cell is selected, its selectedBackgroundView modifies the contents of the contentView. For example, after selecting and/or highlighting a cell, the UILabel in the contentView has its backgroundColor changes and the UIView being used as a cell separator is not visible.

选择之前:

选择后:
< img src =https://i.stack.imgur.com/HlmQk.pngalt =选择/突出显示后>

Before selection: After selection:

我看不到这个行为随处记录。我需要做些什么才能防止这种情况发生?是否有不同的方法来显示细胞选择/突出显示我应采取以防止这种情况?

I don't see this behavior documented anywhere. Is there something I need to do to prevent this? Is there a different approach to showing cell selection/highlighting that I should take to prevent this?


  • 注意:由于它是分组表视图,我用 UIImageView 设置了一个不同的 backgroundView selectedBackgroundView 用于解释 cellForRowAtIndexPath:部分中顶部和底部单元格的圆角,但在使用默认 UITableViewSelectionStyleBlue时遇到同样的问题

  • Note: Since it's a grouped table view, I set a different backgroundViews and selectedBackgroundViews with UIImageViews to account for the rounded corners on the top and bottom cells in the section in cellForRowAtIndexPath:, but I have the same problem when using the default UITableViewSelectionStyleBlue.

编辑1:

每个an0的答案,我覆盖 setHighlighted:animated:。我不确定实现的可靠性,但这种方法可以维护突出显示的 backgroundColor 属性。子视图:

Per an0's answer, I overrode setHighlighted:animated:. I'm not sure how reliable the implementation is, but this approach worked to maintain the highlighted and backgroundColor properties of the subviews:

NSArray *recursiveAllSubviews = [self recursiveValueForKey:@"subviews"]; // Uses MTRecursiveKVC Cocoapod
NSArray *backgroundColors = [recursiveAllSubviews valueForKey:@"backgroundColor"];
[super setHighlighted:highlighted animated:animated];
if (highlighted) {
    [recursiveAllSubviews enumerateObjectsUsingBlock:^(UIView *view, NSUInteger index, BOOL *stop){
        if ([view respondsToSelector:@selector(setHighlighted:)]) {
            [view setValue:[NSNumber numberWithBool:NO] forKey:@"highlighted"];
        }
        id possiblyNull = [backgroundColors objectAtIndex:index];
        if (possiblyNull != [NSNull null]) {
            view.backgroundColor = possiblyNull;
        }
    }];
}


推荐答案

UITableViewCell 在突出显示/选中时自动执行两项操作:

UITableViewCell does two things automatically when highlighted/selected:


  1. 设置其所有子视图' backgroundColor 清除颜色(透明)。

  2. 突出显示所有可突出显示的子视图,例如 UIImageView

  1. Set all its subviews' backgroundColor to clear color(transparent).
  2. Highlight all subviews that can be highlighted, for example, UIImageView.

为防止出现第一个问题,您必须在 UITableViewCell 子类:

To prevent the first problem, you have to override these two methods in your UITableViewCell subclass:

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    [super setHighlighted:highlighted animated:animated];
    if (highlighted) {
        // Recover backgroundColor of subviews.
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    if (selected) {
        // Recover backgroundColor of subviews.
    }
}

这篇关于selectedBackgroundView修改contentView子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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