When a UITableViewCell
has subviews with background color set, when setSelected(_: animated:)
gets called, all the views are made transparent.
This issue is also discussed here
I'm trying to solve this issue by extending UITableViewCell
extension UITableViewCell {
private func backgroundColors(views: [UIView]) -> [UIColor?] {
var colors = [UIColor?]()
for view in views {
colors.append(view.backgroundColor)
}
return colors
}
private func resetBackgroundColors(views: [UIView], colors: [UIColor?]) {
for (index, view) in views.enumerate() {
view.backgroundColor = colors[index]
}
}
func preserveBackgroundColors(views: [UIView], @noescape f: () -> Void) {
let colors = backgroundColors(views)
f()
resetBackgroundColors(views, colors: colors)
}
}
class MyCell: UITableViewCell {
override func setSelected(selected: Bool, animated: Bool) {
preserveBackgroundColors([/* some views */]) {
super.setSelected(selected, animated: animated)
/* rest of method implementation */
}
}
}
I'm trying to avoid having this functionality in a class hierarchy.
Is this a good implementation?