每当我的 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)
}