ios - UITableViewCell 重新排序清除 subview 的背景颜色

标签 ios objective-c iphone uitableview tableviewcell

这个问题已经被问过几次了,但似乎没有任何解决办法。

UITableView reorder hides background

Subviews of UITableViewCell are not visible while reordering

Reordering causing subview 's backround color to clear

我有一个带有 UILabel 的自定义表格 View 单元格。当 tableview 处于编辑模式并且我拖动单元格以重新排序时,UILabel 的背景变为清晰的颜色。我还发现,如果我尝试对选定的单元格重新排序(我的表格 View 在编辑模式下允许多项选择), subview 的背景颜色会保持不变。

我在我的 CustomCell 中尝试了以下方法,但是当单元格被拖动时,它们都没有覆盖 subview 的背景颜色。

我希望保留 subview 的背景颜色。有没有我错过的方法?还是苹果这样设计的?

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    if (selected) {
        if (self.isEditing) {
            self.customLabel.backgroundColor = [UIColor blueColor];
        }
        else {
            self.customLabel.backgroundColor = [UIColor blueColor];
        }
    }
    else {
            self.customLabel.backgroundColor = [UIColor blueColor];
    }
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    [super setHighlighted:highlighted animated:animated];

    if (highlighted) {
        if (self.isEditing) {
            self.customLabel.backgroundColor = [UIColor blueColor];
        }
        else {
            self.customLabel.backgroundColor = [UIColor blueColor];
        }
    }
    else {
        self.customLabel.backgroundColor = [UIColor blueColor];
    }
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];

    if (editing) {
        self.customLabel.backgroundColor = [UIColor blueColor];
    }
    else {
        self.customLabel.backgroundColor = [UIColor blueColor];
    }
}

最佳答案

为 Swift 3 添加现代解决方案。

创建一个新的 .swift 文件并创建一个自定义的 UIView 类:

import UIKit

class NeverClearView: UIView {
    override var backgroundColor: UIColor? {
        didSet {
            if backgroundColor?.cgColor.alpha == 0 {
                backgroundColor = oldValue
            }
        }
    }
}

在 Interface Builder 中,转到 Identity Inspector 并将类设置为 Shiny 的新 NeverClearView 类。这将阻止它在单元格重新排序期间消失。

这个解决方案也适用于其他 UIKit 组件,例如我必须在一秒钟前为 UIStepper 做同样的事情。

关于ios - UITableViewCell 重新排序清除 subview 的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29912845/

相关文章:

ios - Safari 中的居中宽度 div

iphone - 在运行时在本地化 Storyboard之间切换

iphone - 支持 iOS 6 和 armv6

ios - 配置表格每个cell的indexPath

ios - 样式 ActionSheet 的 UIAlertController 显示在状态栏下方

ios - ASIHTTPRequest 多个 POST 参数

ios - 自定义核心数据 SectionNameKeyPath

objective-c - 帮助 - 无休止的 dealloc 循环

iPhone SQLite DB 和基于 Web 的 DB 同步和交互建议

ios - 删除模态 UIView/Partial Curl 的点击识别器