Ios selectedBackgroundView修改contentView子视图
我正在为Ios selectedBackgroundView修改contentView子视图,ios,uitableview,Ios,Uitableview,我正在为UITableViewCell子类使用自定义backgroundView和selectedBackgroundView。这些单元格位于一个分组表中,因此我根据cellforrowatinexpath:中的单元格行,将背景和所选背景设置为UIImageViews 我遇到的问题是,当选择单元格时,其selectedBackgroundView会修改contentView的内容。例如,在选择和/或高亮显示单元格后,contentView中的UILabel的backgroundColor会发生变
UITableViewCell
子类使用自定义backgroundView
和selectedBackgroundView
。这些单元格位于一个分组表中,因此我根据cellforrowatinexpath:
中的单元格行,将背景和所选背景设置为UIImageView
s
我遇到的问题是,当选择单元格时,其selectedBackgroundView
会修改contentView
的内容。例如,在选择和/或高亮显示单元格后,contentView
中的UILabel
的backgroundColor
会发生变化,并且用作单元格分隔符的UIView
不可见
选择前:
选择后:
我在任何地方都没有看到这种行为的记录。我需要做些什么来防止这种情况?是否有其他方法来显示单元格选择/突出显示,以防止出现这种情况
- 注意:由于它是一个分组表视图,因此我使用
s设置了不同的UIImageView
s和backgroundView
s,以说明selectedBackgroundView
中节中顶部和底部单元格的圆角,但是我在使用默认的cellforrowatinexpath:
时也遇到了同样的问题UITableViewSelectionStyleBlue
setHighlighted:animated:
。我不确定实现的可靠性,但这种方法可以维护子视图的突出显示的
和背景色
属性:
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
在突出显示/选中时自动执行两项操作:
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.
}
}
在我的例子中,我的
UITableViewCell
中有两个按钮,它们的背景色正在被清除。这些按钮的类型为GrayButton
,这是我编写的一个自定义类,派生UIButton
。我没有重写UITableViewCell
方法,而是重写了GrayButton
的setBackgroundColor:
方法:
- (void)setBackgroundColor:(UIColor *)backgroundColor
{
if (backgroundColor != [UIColor clearColor]) {
[super setBackgroundColor:backgroundColor];
}
}
设置
cell.selectionStyle=UITableViewCellSelectionStyleNone
和覆盖
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
if(highlighted) {
self.contentView.backgroundColor = colorYouWantWhenHighlighted;
} else {
self.contentView.backgroundColor = colorYouWantWhenUnhighlighted;
}
}
在我的例子中,我切换到contentView,它工作得更好
UIColor *backgroundColor = selected ? [UIColor redColor] : [UIColor clearColor];
self.contentView.backgroundColor = backgroundColor;
而不是替换选定的背景视图
UIView *selectionColor = [[UIView alloc] init];
selectionColor.backgroundColor = selected ? [UIColor redColor] : [UIColor clearColor];;
self.selectedBackgroundView = selectionColor;
[selectionColor release];
现在,仅当您点击并开始滚动的单元格不是所选单元格时,问题才可见 啊,这可能与“如果子视图实现了(如果合适的话)高亮显示属性的访问器方法,那么内容将自动被选择”有关。我的单元格分隔符是使用
backgroundColor
属性实现的,这也解释了它消失的原因。我也可以猜测,因为我做了类似的事情:)如果我想停止1的行为。2.我最好重写setHighlighted:animated:
和setSelected:animated:
方法,而不是调用super,自己实现这些方法吗?除非有一个很好的方法来执行你提到的“恢复背景颜色”步骤。好的,我想出了一个解决方案,并在我的问题中添加了代码。清晰简洁。回答得好。我做了类似的操作,但没有重写方法(需要一个新类等),我只是在tableView:DidSelectRowatineXpath.手动更改了单元格背景。我假设当你点击单元格时,应用程序会立即将你带到下一个屏幕。我怀疑您的方法是否适用于高亮显示状态(例如,将手指放在单元格上,按住并拖动到单元格外,单元格应该没有高亮显示)。你能证实吗?你就快到了。正确的方法不是setHighlighted
,而是setSelected
。