Why do all backgrounds disappear on UITableViewCell select?

前端 未结 8 1855
南笙
南笙 2020-12-02 14:22

My current project\'s UITableViewCell behavior is baffling me. I have a fairly straightforward subclass of UITableViewCell. It adds a few extra elements to the base view (vi

相关标签:
8条回答
  • 2020-12-02 15:01

    Having read through all the existing answers, came up with an elegant solution using Swift by only subclassing UITableViewCell.

    extension UIView {
        func iterateSubViews(block: ((view: UIView) -> Void)) {
            for subview in self.subviews {
                block(view: subview)
                subview.iterateSubViews(block)
            }
        }
    }
    
    class CustomTableViewCell: UITableViewCell {
       var keepSubViewsBackgroundColorOnSelection = false
    
        override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)
        }
    
        // MARK: Overrides
        override func setSelected(selected: Bool, animated: Bool) {
            if self.keepSubViewsBackgroundColorOnSelection {
                var bgColors = [UIView: UIColor]()
                self.contentView.iterateSubViews() { (view) in
    
                    guard let bgColor = view.backgroundColor else {
                        return
                    }
    
                    bgColors[view] = bgColor
                }
    
                super.setSelected(selected, animated: animated)
    
                for (view, backgroundColor) in bgColors {
                    view.backgroundColor = backgroundColor
                }
            } else {
                super.setSelected(selected, animated: animated)
            }
        }
    
        override func setHighlighted(highlighted: Bool, animated: Bool) {
            if self.keepSubViewsBackgroundColorOnSelection {
                var bgColors = [UIView: UIColor]()
                self.contentView.iterateSubViews() { (view) in
                    guard let bgColor = view.backgroundColor else {
                        return
                    }
    
                    bgColors[view] = bgColor
                }
    
                super.setHighlighted(highlighted, animated: animated)
    
                for (view, backgroundColor) in bgColors {
                    view.backgroundColor = backgroundColor
                }
            } else {
                super.setHighlighted(highlighted, animated: animated)
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-02 15:11

    Found a pretty elegant solution instead of messing with the tableView methods. You can create a subclass of UIView that ignores setting its background color to clear color. Code:

    class NeverClearView: UIView {
        override var backgroundColor: UIColor? {
            didSet {
                if UIColor.clearColor().isEqual(backgroundColor) {
                    backgroundColor = oldValue
                }
            }
        }
    }
    

    Obj-C version would be similar, the main thing here is the idea

    0 讨论(0)
提交回复
热议问题