首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >prepareForReuse无法与imageView一起正常工作

prepareForReuse无法与imageView一起正常工作
EN

Stack Overflow用户
提问于 2017-07-10 18:02:00
回答 1查看 401关注 0票数 2

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

代码语言:javascript
复制
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是一个扩展,看起来像这样:

代码语言:javascript
复制
  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函数中的底部边框。我试着用另一个扩展删除它:

代码语言:javascript
复制
 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:

代码语言:javascript
复制
  override func prepareForReuse() {
    self.imageView?.removeBottomBorderWithColor(color: constants.waterblue, width:3)
}
EN

回答 1

Stack Overflow用户

发布于 2017-07-10 18:23:53

代码语言:javascript
复制
self.imageView?.layer.sublayers?.forEach { $0.removeFromSuperlayer() }

尝试将此代码直接放入prepare for reuse方法中。在cellForItemAtIndexPath方法中添加层。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45009142

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档