每当我的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)
}发布于 2017-07-10 18:23:53
self.imageView?.layer.sublayers?.forEach { $0.removeFromSuperlayer() }尝试将此代码直接放入prepare for reuse方法中。在cellForItemAtIndexPath方法中添加层。
https://stackoverflow.com/questions/45009142
复制相似问题