我使用的是定制的backgroundView,并selectedBackgroundView为一个UITableViewCell子类.这些单元格位于分组表中,因此我UIImageView根据单元格的行将背景和选定背景设置为s cellForRowAtIndexPath:.
我遇到的问题是,当选择单元格时,它selectedBackgroundView会修改单元格的内容contentView.例如,在选择和/或突出显示单元格之后,其中UILabel的内容contentView发生了backgroundColor变化,UIView并且用作单元格分隔符的内容不可见.
选择前:
选择后:

我没有在任何地方看到这种行为.我需要做些什么才能防止这种情况发生?是否有不同的方法来显示细胞选择/突出显示我应采取以防止这种情况?
backgroundViews和selectedBackgroundViews与UIImageViews来说明该部分中顶部和底部单元格的圆角cellForRowAtIndexPath:,但是在使用默认值时我遇到了同样的问题UITableViewSelectionStyleBlue.编辑1:
根据an0的回答,我超越了setHighlighted:animated:.我不知道如何实施可靠的,但是这种方法的工作,以保持highlighted和backgroundColor子视图的属性:
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;
}
}];
}
Run Code Online (Sandbox Code Playgroud)
an0*_*an0 45
UITableViewCell 突出显示/选中时自动执行两项操作:
backgroundColor为清除颜色(透明).UIImageView.要防止出现第一个问题,必须在UITableViewCell子类中覆盖这两个方法:
- (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.
}
}
Run Code Online (Sandbox Code Playgroud)
cell.selectionStyle = UITableViewCellSelectionStyleNone- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
if(highlighted){
self.contentView.backgroundColor = colorYouWantWhenHighlighted;
}else{
self.contentView.backgroundColor = colorYouWantWhenUnhighlighted;
}
}