首页 > 解决方案 > 为什么在覆盖自定义单元格的 setSelected 方法后,表格视图单元格每次在滚动时出现时都会被选中?

问题描述

我尝试为表格视图单元格选择制作自定义动画,因此我使用以下代码覆盖了自定义单元格的 setSelected 方法:

  override func setSelected(_ selected: Bool, animated: Bool) {

    if self.frame.size.height == 132{
        self.backgroundViewTop.constant = 40
        UIViewPropertyAnimator.runningPropertyAnimator(withDuration: 0.08, delay: 0, options: [.curveEaseIn], animations: {
            self.backView.backgroundColor = UIColor(red: 0.891, green: 0.9, blue: 0.9, alpha: 1)
        }, completion: { finished in
            UIViewPropertyAnimator.runningPropertyAnimator(withDuration: 0.2, delay: 0, options: [.curveEaseIn], animations: {
                self.backView.backgroundColor = UIColor.white
            }, completion: nil )
        })
    }else{
        self.backgroundViewTop.constant = 0
        UIViewPropertyAnimator.runningPropertyAnimator(withDuration: 0.08, delay: 0, options: [.curveEaseIn], animations: {
            self.backView.backgroundColor = UIColor(red: 0.891, green: 0.9, blue: 0.9, alpha: 1)
        }, completion: { finished in
            UIViewPropertyAnimator.runningPropertyAnimator(withDuration: 0.2, delay: 0, options: [.curveEaseIn], animations: {
                self.backView.backgroundColor = UIColor.white
            }, completion: nil )
        })
    }

}

现在,当我滚动屏幕上出现新的单元格时,就会出现这个动画。为什么要调用此方法,并以正确的方式覆盖它以在选择时实现自定义动画?

标签: iosswifttableview

解决方案


推荐阅读