0

I am drawing a circle in cellForItemAt method as follows for a particular condition:

    if cellState.date == CalendarModel.DUE_DATE {
        let shapeLayer = CAShapeLayer()

        let circlePath = UIBezierPath(arcCenter: CGPoint(x: (cell.layer.frame.size.width)/2,y: (cell.layer.frame.size.height)/2), radius: CGFloat(15), startAngle: CGFloat(0), endAngle:CGFloat(Double.pi * 2), clockwise: true)


        shapeLayer.path = circlePath.cgPath

        //change the fill color
        shapeLayer.fillColor = UIColor.clear.cgColor
        //you can change the stroke color
        shapeLayer.strokeColor = UIColor.FlatColor.Blue.midnightBlue.cgColor
        //you can change the line width
        shapeLayer.lineWidth = 1.0
        cell.layer.addSublayer(shapeLayer)
    }

The circle keeps repeating randomly. I'm trying the following code in willDisplayCell however it removes all content from the cell.

func calendar(_ calendar: JTAppleCalendarView, willDisplay cell: JTAppleCell, forItemAt date: Date, cellState: CellState, indexPath: IndexPath) {
    // comment
    cell.layer.sublayers?.forEach { $0.removeFromSuperlayer() }
}

How to specifically remove the shapeLayer that was added in cellForItemAt? Any help would be greatly appreciated. Thank you.

as diu
  • 1,010
  • 15
  • 31
  • 1
    You need to create a subclass of JTAppleCell and add a property so that you can keep a reference to the new layer. Then you can easily remove that layer if it isn’t needed when the cell is reused. – Paulw11 Nov 27 '17 at 19:39
  • @Paulw11 Thank you, I added a property to the subclass called shapeLayer. Another method to same class `addCircle()` which does adding the circle. Now in `cellForItemAt` I'm trying to set `cell.shapeLayer = nil` in else condition after dequeue line. still the circles are repeating in other months when I scroll. – as diu Nov 27 '17 at 19:49
  • 1
    No, setting it to nil isn’t enough. That will nil the property but won’t remove it. You need to remove the layer if it isn’t nil in an else from your if. `cell.circleLayer?.removeFromSuperlayer()` and then set it to nil – Paulw11 Nov 27 '17 at 20:04

1 Answers1

0

as per @Paulw11 's suggestion, I subclassed the JTAppleCell and added a variable as follows:

    var shapeLayer : CAShapeLayer!

Then added another method in the same JTAppleCell's subclass as below:

func addCircle() {
    self.shapeLayer = CAShapeLayer()
    let circlePath = UIBezierPath(arcCenter: CGPoint(x: (self.layer.frame.size.width)/2,y: (self.layer.frame.size.height)/2), radius: CGFloat(15), startAngle: CGFloat(0), endAngle:CGFloat(Double.pi * 2), clockwise: true)
    shapeLayer.path = circlePath.cgPath
    shapeLayer.fillColor = UIColor.clear.cgColor
    shapeLayer.strokeColor = UIColor.FlatColor.Blue.cgColor
    shapeLayer.lineWidth = 1.0
    self.layer.addSublayer(shapeLayer)
}

in cellForItemAt I added the following lines after dequeueing the cells in an else block:

if cell.shapeLayer != nil {
      cell.shapeLayer.removeFromSuperlayer()
      cell.shapeLayer = nil
}

Thank you @Paulw11 the above solution worked for me.

as diu
  • 1,010
  • 15
  • 31