Ios 在UITableView中加载新数据时重叠图层

Ios 在UITableView中加载新数据时重叠图层,ios,swift,uitableview,tableviewcell,custom-view,Ios,Swift,Uitableview,Tableviewcell,Custom View,我正在绘制时间轴视图。当我更新上一层底部绘制的圆圈层的新数据时,我正在使用UITableView 这是加载新数据时的外观 这是我的自定义单元格代码: override func draw(_ rect: CGRect) { if let anchor = anchorView() { anchor.layer.zPosition = 3 let iconImageView = anchor as! UIImageView

我正在绘制时间轴视图。当我更新上一层底部绘制的圆圈层的新数据时,我正在使用
UITableView

这是加载新数据时的外观

这是我的自定义单元格代码:

  override func draw(_ rect: CGRect) {
      if let anchor =  anchorView() {
          anchor.layer.zPosition = 3
          let iconImageView = anchor as! UIImageView
          iconImageView.image = iconImageView.image!.withRenderingMode(UIImageRenderingMode.alwaysTemplate)
          iconImageView.tintColor = UIColor.white

          let centreRelativeToTableView = anchor.superview!.convert(anchor.center, to: self)
          // print("Anchor x origin : \(anchor.frame.size.origin.x)")
          timelinePoint.position = CGPoint(x: centreRelativeToTableView.x , y: centreRelativeToTableView.y/2)
          timeline.start = CGPoint(x: centreRelativeToTableView.x  , y: 0)
          timeline.middle = CGPoint(x: timeline.start.x, y: anchor.frame.origin.y)
          timeline.end = CGPoint(x: timeline.start.x, y: self.bounds.size.height)
          timeline.draw(view: self.contentView)

          let circlePath = UIBezierPath(arcCenter: CGPoint(x: anchor.center.x,y: anchor.center.y ), radius: CGFloat(20), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)

          let shapeLayer = CAShapeLayer()
          shapeLayer.path = circlePath.cgPath

          //change the fill color
          shapeLayer.fillColor = UIColor.randomFlat.cgColor
          // shapeLayer.fillColor = UIColor.clear.cgColor

          //you can change the stroke color
          shapeLayer.strokeColor = UIColor.white.cgColor
          //you can change the line width
          shapeLayer.lineWidth = 0
          anchor.alpha = 1
          //Set Anchor Z position
          anchor.layer.zPosition = 2
          shapeLayer.zPosition = 1

          anchor.superview?.layer.insertSublayer(shapeLayer, at: 0   )
          //  anchor.tintColor = UIColor.white

          // Setting icon to layer
          /* let imageLayer = CALayer()
          imageLayer.contents = iconImageView.image
          imageLayer.frame = anchor.frame
          anchor.layer.addSublayer(imageLayer)*/
          // timelinePoint.draw(view: self.contentView)
          } else {
                print("this should not happen")
      }
  }
我怎样才能解决这个问题

itemImageView.layer.cornerRadius = 25;
更新:正如用户atulkhatri所指出的,如果不添加以下内容,它将无法工作:

itemImageView.layer.masksToBounds = YES;

在此扩展名中调用图像视图

    extension UIImageView {
        public func maskCircle(anyImage: UIImage){
        self.contentMode = UIViewContentMode.ScaleAspectFill
        self.layer.cornerRadius = self.frame.height / 2
        self.layer.masksToBounds = false
        self.clipsToBounds = true
        //make square(* must to make circle), resize(reduce the kilobyte) & fix rotation. 
        self.image = prepareImage(anyImage)
        }
    }

CAShapeLayerFrame的设置框架不是问题。当数据加载和单元格高度更改时,会发生这种情况。所以锚的位置改变了。那么我如何删除以前绘制的图层呢?您可以使用removefromsuperview进行删除。在设置响应之前,请删除图层或尝试viewToClear.layer.sublayers=nil;应用程序在以下行崩溃:anchor.superview!。转换(anchor.center,to:self)您将在modijecky@gmail.com如果你不介意的话,我无法理解你的问题?所以在这之后我会看到你的问题并解决你的问题