selectedBackgroundView修改contentView子视图 [英] The selectedBackgroundView modifies the contentView subviews
问题描述
我正在使用自定义 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 UIImageView
s 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
backgroundView
s andselectedBackgroundView
s withUIImageView
s to account for the rounded corners on the top and bottom cells in the section incellForRowAtIndexPath:
, but I have the same problem when using the defaultUITableViewSelectionStyleBlue
.
编辑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:
- 设置其所有子视图'
backgroundColor
清除颜色(透明)。 - 突出显示所有可突出显示的子视图,例如
UIImageView
。
- Set all its subviews'
backgroundColor
to clear color(transparent). - Highlight all subviews that can be highlighted, for example,
UIImageView
.
为防止出现第一个问题,您必须在 UITableViewCell $中覆盖这两种方法c $ c>子类:
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屋!