2

每当我的 isLive 函数返回 true 时,我都会尝试向图像视图添加底部边框。我检查了该部分,它只执行一次。但是,有多个带有蓝色底边框的单元格。此代码如下所示:

DispatchQueue.main.async {
    if CommunityViewController().isLive(startTime: startTime, endTime: endTime, name: self.cellChannel[indexPath.row].communityName){
        cell.imageView.addBottomBorderWithColor(color: constants.waterblue, width: 3)
        }
    }

我的 addBottomBorderWithColor 是一个扩展,看起来像这样:

  func addBottomBorderWithColor(color: UIColor, width: CGFloat) {
    let border = CALayer()
    border.backgroundColor = color.cgColor
    border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width, height: width)
    self.layer.addSublayer(border)
}

据我所知,我需要删除 prepareForReuse 函数中的底部边框。我尝试使用其他扩展名将其删除:

 func removeBottomBorderWithColor(color: UIColor, width: CGFloat) {
    let border = CALayer()
    border.backgroundColor = color.cgColor
    border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width, height: width)
    self.layer.addSublayer(border)
    border.removeFromSuperlayer()
}

prepareForReuse 是正确的方法还是我需要以其他方式设置它?

我通过以下方式调用 prepareForReuse :

  override func prepareForReuse() {
    self.imageView?.removeBottomBorderWithColor(color: constants.waterblue, width:3)
}
4

1 回答 1

1
self.imageView?.layer.sublayers?.forEach { $0.removeFromSuperlayer() }

尝试将此代码直接放入准备重用方法中。在 cellForItemAtIndexPath 方法中添加图层。

于 2017-07-10T10:23:52.937 回答