gpt4 book ai didi

ios - selectedBackgroundView 修改 contentView subview

转载 作者:IT王子 更新时间:2023-10-29 07:58:46 27 4
gpt4 key购买 nike

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

我遇到的问题是,当单元格被选中时,它的 selectedBackgroundView 修改了 contentView 的内容。例如,在选择和/或突出显示单元格后,contentView 中的 UILabelbackgroundColor 发生变化,UIView 被用作单元格分隔符是不可见的。

选择前: Before selection/highlight选择后: After selection/highlight

我没有在任何地方看到这种行为的记录。我需要做些什么来防止这种情况发生吗?是否有不同的方法来显示我应该采取的单元格选择/突出显示来防止这种情况发生?

  • 注意:因为它是分组 TableView ,所以我设置了不同的 backgroundViewselectedBackgroundView 以及 UIImageView 来考虑四舍五入cellForRowAtIndexPath: 部分中顶部和底部单元格的角,但在使用默认 UITableViewSelectionStyleBlue 时我遇到了同样的问题。

编辑 1:

根据 an0 的回答,我覆盖了 setHighlighted:animated:。我不确定实现的可靠性如何,但这种方法可以维持 subview 的 highlightedbackgroundColor 属性:

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 在高亮/选中时自动做两件事:

  1. 将其所有 subview 的backgroundColor设置为清除颜色(透明)。
  2. 高亮所有可以高亮的 subview ,例如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.
}
}

关于ios - selectedBackgroundView 修改 contentView subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14468449/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com