具有清晰背景和彩色contentView的UITableViewCell

时间:2019-03-04 15:09:09

标签: ios swift uitableview

我想在UITableViewCell周围创建一个“假”间距,我这样做是通过将contentView的框架各插入10个[我实际上是在contentView和插图的顶部添加了一个自定义视图10]。看起来contentView是唯一可见的视图。这看起来真的很好,我也正在为单元格设置和调整selectedBackgroundView的框架,以便选择将仅选择“可见”区域。

现在这样做的问题如下:

如果我选择一个单元格,它会以UIColor.darkGray所指定的selectedBackgroundView闪烁。 然后在动画中的一小段时间内,我的单元格背景完全不可见,然后又闪回原样。 这样,动画看起来就不会流畅。

这适用于内容视图:

  1. 背景色
  2. 暗灰色(selectedBackgroundView)
  3. 清除颜色
  4. 背景色

有人知道我是否可以在保持选择不变的同时解决此问题?

我从动画中创建了一个gif:https://imgur.com/vkfA62w

这是我的代码:

class BasicTableViewCell : UITableViewCell {
    public var basicContentView: UIView = UIView(frame: .zero)

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: .default, reuseIdentifier: reuseIdentifier)

        self.tintColor = UIColor.white

        self.contentView.backgroundColor = UIColor.clear

        self.basicContentView.backgroundColor = UIColor.barTintColor
        self.basicContentView.layer.masksToBounds = false
        self.basicContentView.layer.cornerRadius = 10.0
        self.basicContentView.translatesAutoresizingMaskIntoConstraints = false
        self.contentView.addSubview(self.basicContentView)

        self.basicContentView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 10.0).isActive = true
        self.basicContentView.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor, constant: -10.0).isActive = true
        self.basicContentView.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 10.0).isActive = true
        let bottomConstraint = self.basicContentView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: -10.0)
        bottomConstraint.priority = UILayoutPriority(999)
        bottomConstraint.isActive = true

        let selectedView: UIView = UIView(frame: .zero)
        selectedView.backgroundColor = UIColor.darkGray
        selectedView.layer.cornerRadius = 10.0
        selectedView.layer.masksToBounds = false
        self.selectedBackgroundView = selectedView
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        // only for selectedBackgroundView, contentView raised other issues
        let contentViewFrame = self.contentView.frame
        let insetContentViewFrame = contentViewFrame.inset(by: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
        self.selectedBackgroundView?.frame = insetContentViewFrame
    }
}

基于Apples explanation,我已经知道问题所在了 selectedBackgroundView被添加到contentView,然后逐渐消失,然后从contentView中删除,然后突然再次看到basicContentView,这会导致该错误。

这是我的解决方法

override func setSelected(_ selected: Bool, animated: Bool) {
        if selected {
            self.basicSelectedBackgroundView.alpha = 1.0
            self.basicContentView.insertSubview(self.basicSelectedBackgroundView, at: 0)
        } else {
            guard self.basicSelectedBackgroundView.superview != nil else {
                return
            }
            if animated {
                UIView.animate(withDuration: 0.5, delay: 0.0, options: .allowUserInteraction, animations: {
                    self.basicSelectedBackgroundView.alpha = 0.0
                }) { (finished: Bool) in
                    if finished {
                        self.basicSelectedBackgroundView.removeFromSuperview()
                    }
                }
            } else {
                self.basicSelectedBackgroundView.alpha = 0.0
                self.basicSelectedBackgroundView.removeFromSuperview()
            }
        }
    }

我将覆盖setSelected并对其进行动画处理。

1 个答案:

答案 0 :(得分:0)

您可能想覆盖单元格中的setSelected。例如:

override func setSelected(_ selected: Bool, animated: Bool) {
    //If we don't get the contentView's backgroundColor here and then reset it after the call to super.setSelected, the contentView's backgroundColor will "disappear" when the cell is selected
    let contentViewColor = contentView.backgroundColor
    super.setSelected(selected, animated: animated)
    contentView.backgroundColor = contentViewColor
}

选中一个单元格后,该单元格所有子视图的backgroundColor属性将设置为选择颜色,该颜色将覆盖您自己设置的所有backgroundColor属性。因此,您可能需要在此处手动设置contentView的backgroundColor以获得所需的行为。